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 the user for a word. Then put a dash between every character in the word. Then print the word.

answer

import java.util.*;

public class WordDasher

{ public static void main()

{ Scanner screen = new Scanner(System.in);

System.out.println("Enter word");

String myword = screen.next();

System.out.print(myword.charAt(0));

for (int count = 1; count < myword.length(); count++)

{

System.out.print("-" +myword.charAt(count));

}

System.out.println();

}

}

4)Sum up numbers 9 to 200 counting by 2’s

answer

int sum = 0;

for (int count = 9; count <= 200; 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);

5)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)

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

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

double profit = myitem.(10,50);

The student 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 priceIn)

{

return this.qty * (priceIn-this.cost);

}

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

double profit = myitem.(10,50);

answerthis.cost = 10.; this.price = price = 20; priceIn = 50

b. What is the value of profit after double profit = myitem.(10,50) runs?

answer400 (10*40)

7)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;

}

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

answer

public void updateGpa(double newGpa)

{

this.gpa = newGpa;

}

9)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);

}

}

10)An exercise to create and use a class

  • 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.
  • Create another program to use the bikes that does the following in order:
  • Ask the user for the color of 2 bikes, and create those 2 bikes, both with a speed of 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.
  • 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.
  • Print the color and speed of each bike (by asking the bikes).
  • 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.
  • 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

To be supplied

11)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

e)