Question
1. The while loop below prints a sequence of digits in a row without spaces. In the space provided, enter this sequence. int k =
1. The while loop below prints a sequence of digits in a row without spaces. In the space provided, enter this sequence.
int k = 1; while(k < 10) { k = k + 2; System.out.print(k); }
2. The while loop below prints a sequence of digits in a row without spaces. In the space provided, enter this sequence.
int k = 9; while(k > 0) { k = k - 3; System.out.print(k); }
3. Examine the code fragment below. What will it print? Type your answer in the answer below.
String theString = "That's too tricky to tell!"; int tCount = 0; for (int i = 0; i < 8; i++) { char c = theString.charAt(i); switch(c) { case 't': tCount++; break; } } System.out.print(tCount);
4. Write a chained if-else statement that captures the following scenario: Suppose r is a rate for a mortgage loan. If r is less than 2.5% (.025) then print "great"; if r is at least 2.5% but less than 4%, print "good"; and if r is 4% or higher print "not great".
5. The method stringRoot below is passed a string of digits as a parameter (no decimal point), and returns the square root of the number represented by those digits. Complete the implementation of the method. Example "1600" --> 40.0 public double stringRoot(String str) {
6. Suppose that you are given the following PetDriver class, which includes a main method:
public class PetDriver{ public static void main(String[] args){ int weight = 40; Pet doggie = new Pet("Rover", weight); System.out.println("my pet's name is " + doggie.getName()); System.out.println("my pet's weight is " + doggie.getWeight()); } }
Executing main produces the following output: my pet's name is Rover my pet's weight is 40 Complete the Pet class definition, below, so that the main method shown above works correctly. (Hint: if you're stuck, look over the definition of the Infant class. The Pet class definition is very similar.) public class Pet {
7. Suppose that you are given the following Driver class, which includes a main method:
public class Driver { public static void main(String[] args) { double distance = 400.48; double fuel = 21.4; AutoTrip myTrip = new AutoTrip(distance, fuel); System.out.print("My car traveled " + myTrip.getDistance() + " miles "); System.out.println("on " + myTrip.getFuel() + " gallons of gasoline."); double mileage = myTrip.getMPG(); // get miles per gallon System.out.println("My mileage was " + mileage + "."); } }
Now suppose that executing main produces the following output: My car traveled 400.48 miles on 21.4 gallons of gasoline. My mileage was 18.714018691588787. Implement the AutoTrip class so that it produces the indicated output. public class AutoTrip {
8. Write a public method called halfCount, which is passed an int as a parameter and which performs successive (integer) divisions by 2 until 0 is reached. The method returns the number of divisions required until 0 is reached. Example: 2 --> 2 (2/2 = 1, then 1/2 = 0, so two successive divisions by 2 get you to zero) Example: 10 --> 4 (10/2 = 5; 5/2 = 2; 2/2 = 1; 1/2 = 0) (Hint: use a while loop in the body, and set up the loop so that at each loop iteration, the input value is halved. Use an auxiliary variable to count the number of loop iterations. When the loop finishes, return this count.) Note that in this example you must write a complete method, including the method header line.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started