setWeight method which
does no more than assign the value of a double parameter called
weight to the corresponding instance variable.
getWeight. This method should
be accessible from outside its class, and it should return the
current value of an instance variable called weight, which is
declared to be a double.
What are values of gus.age, gus.weitght, and
gus.precentGrowthRate at the end of the exection of the below
main() method.
public class Mouse2Driver
{
public static void main(String[] args)
{
Mouse2 gus = new Mouse2();
gus.setPercentGrowthRate(5);
gus.grow();
} // end main
} // end class Mouse2Driver
public class Mouse2
{
private int age = 0; // age of mouse in days
private double weight = 1.0; // mouse weight in grams
private double percentGrowthRate; // increase per day
public void setPercentGrowthRate(double percentGrowthRate)
{
this.percentGrowthRate = percentGrowthRate;
} // end setPercentGrowthRate
public void grow()
{
this.weight +=
(.01 * this.percentGrowthRate * this.weight);
this.age++;
} // end grow
} // end class Mouse2
Write a Java method returns a random boolean variable which returns
true half the time and false the rest of the time. Use the
random() method in the Math class which returns random double
numbers between 0 and 1. Below is a stub.
public static boolean randomBoolean(){
}
Assume you have a Car class that declares two private instance
variables, String make; and String model. Write Java code that
implements a two-parameter constructor that instantiates a Car
object and initializes both of its instance variables.
Practice your pseudocode skills using the following exercises. Answers will be posted over the weekend.
where
,
,
, and
are given as inputs.
![]() | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
![]() | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 |
and then print the nth
Fibonacci number.
The views and opinions expressed in this page are strictly those of the page author.
The contents of this page have not been reviewed or approved by the University of Minnesota.