Review exercises:

 

1)      What word in the method header below means that any class can use this method:

                        public static int methodAnyOneCanUse (int all)

answerà public

 

2)      Write an expression that is :

a)      true for -3, -20 and 30,000

b)      false for -1 and 10,000

Note: write one expression that satisfies all the conditions above

answerà(x < -1 || x > 29,000)

 

3)      Ask a user for a number. Sum up numbers 9 to the number the user entered, counting by 2’s

 

answerà

import java.util.Scanner;

public class Number3{

public static void main(){

Scanner myscan = new Scanner(System.in);

System.out.println("Enter a number");

int stop = myscan.nextInt();

int sum = 0;

for (int count = 9; count <= stop; count = count+2)

{

       System.out.println("I am adding in " + count + " for a sum of " + sum);

       sum = sum + count;

}

System.out.println("The sum is " + sum);

4)      Just write the start of the loop that keeps going as long as the salary variable value is greater than 500 and the taxFlag is true.

answerà

while(salary> 500 && taxFlag)

 

5)      When the main method of a BillManager class executes:

Item myitem = new Item(“keyboards”,10.)

double profit = myitem.calcProfit(10,50);

 

AND The Item class is

public class Item
{

private String name;

private double cost;
private double price;
public Item(String nameIn, double costIn)

{

this.name = nameIn;

this.cost = costIn;

this.price = this.cost*2;

}

public double calcProfit(int qty, double priceActuallySold)

{

return  qty * (priceActuallySold-this.cost);

}
 

a)      What is the value of this.cost in the calcProfit method when it is called by

double profit = myitem.calcProfit(10,50);

answeràthis.cost = 10.; this.price = price = 20; priceActuallySold = 50

b) What is the value of profit after double profit = myitem.calcProfit(10,50) runs?
answerà400 (10*40)

 

6)      Add a method to the student class to return the letter grade associate with their GPA. If the GPA is 3.0 or better, return A, 2.0 or better, return B, 1.0 or better, return C.

The student class is:

public class Student
{
      private String name;

      private double gpa;
      public Student(String nameIn)
      {
            this.name = nameIn;

            this.gpa = 0;

      }

answerà

public char getLetter()

{

            char letter;

            if (this.gpa >=3)
            {          letter = 'A';}

             else if (this.gpa >=2)

            {          letter = 'B';}

             else 

            {          letter = 'C';}

return letter;

}

 

 

 

7)      Add a method to update the gpa to the student class above.

answerà

public void updateGpa(double newGpa)

{

        this.gpa = newGpa;

}

 

8)      Create a program to ask a student for their name and create student objects using the class above. Then, ask their gpa and update each student’s GPA. Then call the method that returns the letter grade for every student’s gpa to a letter grade and print their grades.   

answerà

import java.util.*;

public class MakeStudents

{

            public static void main()

            {

                        Scanner screen = new Scanner(System.in);

                        System.out.println("Enter student name");

                        String name = screen.next();

                        Student myStudent = new Student(name);

                        System.out.println("Enter their gpa");

                        double gpaIn = screen.nextDouble();

                        myStudent.updateGpa(gpaIn);

                        char grade = myStudent.getLetter();

                        System.out.println("your grade is " + grade);

            }

}

                       

                       

 

9)      An exercise to create and use a class

o   Create a class of bikes with a color and number of speeds. Create a constructor and a method to update each variable and another to give back each variable.

o   Create another program to use the bikes that does the following in order:

o   Ask the user for the color of 2 bikes, and create those 2 bikes, both with a speed of 3.

o   Add 1 to the number of speeds of the first bike and print the new speed 6 times and print the total of all the printed speeds.

o   Add 3 to the number of speeds of the second bike continuously until the number of speeds exceeds 20, and print the count of how many times the speed changed.

o   Print the color and speed of each bike (by asking the bikes).

o   Multiply the speed of each bike by $10 to determine the selling price. Add a 5% tax. Print the final price, the tax rate, and the tax amount.

o   If the final price is equal to $70 or if it is less than $25, print "low"; if it is less than $45 print "ok"; if it is anything else, print "bad".

answerà

public class Bike{
private String color;
private int speeds;
public Bike(String colorin, int speedin)
{ this.color = colorin; this.speeds = speedin;}
public int getSpeed(){return this.speeds;}
public void udpateSpeed(int speedin){this.speeds = speedin;}
public String getColor() {return this.color;}
public void updateColor(String colorin){this.color = colorin;}}

import java.util.Scanner;
public class MakeBikes{
public static void main(){
String colorAnswer;
int speedAnswer;
System.out.println("What is the color of the bike?")
colorAnswer = scan.next();
Bike one = new Bike(colorAnswer, 3);
System.out.println("What is the color of the bike?")
colorAnswer = scan.next();
Bike two = new Bike(colorAnswer, 3);
// Add 1 to the number of speeds of the first bike and print the new speed 6 times and print the total of all the printed speeds.
one.updateSpeed(one.getSpeed()+1);
int total = 0;
for (int x = 1; x <=6; x++){System.out.println(one.getSpeed()); total = total + one.getSpeed();}
System.out.println(total);
//Add 3 to the number of speeds of the second bike continuously until the number of speeds exceeds 20,
//         and print the count of how many times the speed changed.
int count = 0;
while (two.getSpeed() < 20){
two.updateSpeed(two.getSpeed()+3);
count = count+1;}
System.out.println(count);
//Print the color and speed of each bike (by asking the bikes).
System.out.println("Bike 1 is " + one.getColor() + " color and " + one.getSpeed() + " speed and Bike 2 is " + two.getColor() + " color and " + two.getSpeed() + " speed.");
// Multiply the speed of each bike by $10 to determine the selling price. Add a 5% tax. Print the final price, the tax rate, and the tax amount.
double price1 = one.getSpeed()*10*1.05;
double price2 = two.getSpeed()*10*1.05;
System.out.println ("The final price for one is " + price1 + " the tax rate is 5% and the tax amount is " + (one.getSpeed()*10*.05));
System.out.println ("The final price for one is " + price2 + " the tax rate is 5% and the tax amount is " + (two.getSpeed()*10*.05));
if (price2 == 70 || price2 < 25){
System.out.println ("The price of bike 2 is low.");}
else if (price2 < 45) {
System.out.println ("The price of bike 2 is ok.");}
else {
System.out.println ("The price of bike 2 is bad.");}

10)  Add to your game:

a)      Count the number of turns to win

b)      Determine the average roll

c)      print a message based on the count:

i)        print "quick" if count is less than 5

ii)      print "good" if count is less than 10 or exactly 25

iii)    print "long game" if count is greater than or equal to 10, but not 25

d)     ask if the user wants to play a new game when they are done : Determine the average

No Answer will be supplied, but email if you are unsure of your answer.

11)  Arrays

a)      Create an array of 5 numbers

b)      Fill that array with 5 values

c)      Total those 5 values

d)     Print the total

answerà

 

int[] tester = new int[5];
tester[0] = 3; tester[1] = 4; tester[2] = 33; tester[3] = 35; tester[4] = 77;

int total = 0;
for (int count = 0; count < tester.length; count++){

total = total + tester[count];}
System.out.println("The total is " + total);