Review exercises:
1) What word in the method header below means that any class can use this method:
public static int methodAnyOneCanUse (int all)
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
3) Ask a user for a number. Sum up numbers 9 to the number the user entered, counting by 2’s
4) Just write the start of the loop that keeps going as long as the salary variable value is greater than 500 and the tax flag is true.
5) When the main method of a BillManager class executes:
Item myitem = new Item(“keyboards”,10.)
double profit = myitem.calcProfit(10,50);
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);
b) What is the value of profit after double profit = myitem.calcProfit(10,50) runs?
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 int
gpa;
public Student(String nameIn)
{
this.name = nameIn;
this.gpa = 0;
}
7) Add a method to update the gpa to the student class above.
8) Create a program to ask students 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.
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".
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
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