Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class LotsOfMethods { public static void main(String [] args) { // Sample

ID: 3741085 • Letter: P

Question

 public class LotsOfMethods
 {
         public static void main(String [] args)
         {
                // Sample method getting called.  Just reports the String sent to the method
                // by printing "Received: " followed by the String.
                
                sampleMethod("hello");
                sampleMethod("there");
                
                // For the received integer value, report whether or not it is odd.
                // It should work for any integer value (not just those used below).
                // It reports a message of the format "16 is not odd" or "1 is odd".
                
                reportOdd(3);
                reportOdd(100);
                reportOdd(77);
                
                // Assume that the two values represent the width and height of
                // a rectangle.  Report the area of the rectangle with a line of 
                // the form "Area is 100.0 for a rectangle of width 10.0 and height 10.0".
                // Print one digit after the decimal point.
                
                printArea(10.00, 10.0);
                printArea(3.555, 19.4);
                printArea(600.5, 1.1);
                
                // Assume that the values sent to stockSale are a String representing
                // the name of the company, an integer representing the number of shares
                // of the stock that were sold, and the price the stock was sold at.
                // Report the information about the sale in a line of output of this 
                // format:
                //
                //       Sold 50 shares of IBM at $35.50 for $1,775.00
                // 
                
                stockSale("IBM", 50, 35.5);
                stockSale("Apple", 100, 440.25);
                stockSale("GM", 25, 12.75);
                
                // Write the method determineTax.  The method is sent the amount of the
                // purchase and the tax rate.  It returns a double value that is the 
                // amount of the tax.
                // Note that the method determineTax does not print anything -- it 
                // just returns the value.
                
                double m1 = determineTax(350.00, 0.06);  // Michigan is 6%
                double m2 = determineTax(79.99, 0.06);
                
                double cal1 = determineTax(499.99, 0.0725);  // California is 7.25%
                
                System.out.printf("Here are the returned values: %.2f, %.2f, and %.2f ", m1, m2, cal1);
                
                // Reports the grade that corresponds to the test score supplied, according to this scale:
                // A: 100-90, B: 89-80, C: 79-70, D: 69-60, E: 59-0.  Report "Invalid" for any other score.
                // Invalid scores should be reported like this: "110 is an invalid score". 
                // Valid scores should be reported like this: "70 has a grade of C".
                
                testScore(93);
                testScore(70);
                testScore(110);
                testScore(-3);
                testScore(52);
                testScore(88);
                
                // In the gambling game of craps the gambler rolls two dice.  The first roll of the dice 
                // has several different outcomes.  If a 2, 3, or 12 is rolled then the gambler "craps out" (i.e, 
                // they lose).  If they roll a 7 or 11 then they "win".  If they roll any other number then
                // they "keep rolling". 
                // Write a method that takes the two dice and reports one of the three possible outcomes:
                //   * "crap out"
                //   * "win"
                //   * "keep rolling"
                //
                // This method just reports the status of just the first roll.
                
                firstRoll(2, 3);
                firstRoll(6, 1);
                firstRoll(4, 5);
                firstRoll(4, 3);
                firstRoll(6, 6);
                
                // Write a method waterState that takes a single double value as a parameter that indicates the 
                // temperature of the water.  Your method will return a single String indicating the state of water
                // at that temperature.  The three possible states are "solid", "liquid", or "gas". I.e., water is 
                // solid, as ice, at 32 degrees or less, it is a gas (steam) at 212 degrees and above, and it is a 
                // liquid between 32 and 212.
                
                String w1 = waterState(76.5);  // 76 degrees -- liquid water
                System.out.println("w1 is: " + w1);
                
                String w2 = waterState(17.7);
                System.out.println("For 17 it is: " + w2);
                
                String w3 = waterState(32.0);
                System.out.println("Right at 32 is is frozen: " + w3);
                
                System.out.println("Hot: " + waterState(250.0));
                
          // Write a method that returns a boolean value of true or false.
         // It takes three test scores, and return true or false to indicate
          // if the average grade is a passing grade (i.e., return true if the average is
                // 70.0 points or more).
                
                boolean b = passes(99.0, 83.0, 90.2);
                System.out.println("b is: " + b);
                
                boolean b2 = passes(72.0, 54.5, 69.5);
                System.out.println("return value of: " + b2);
                
                System.out.println( passes(100.0, 5.0, 82.0) );
                
                // Write a method that uses a loop to print the squares of numbers.
                // The squares are reported in increments of one from the first 
                // number to the second number, inclusive:
                
                squares(2, 9);
                squares(100, 105);
                squares(22, 30);
         }

Explanation / Answer

LotsOfMethods.java

public class LotsOfMethods

{

public static void sampleMethod(String s) {

System.out.println("Received: "+s);

}

public static void reportOdd(int n) {

if(n % 2 != 0) {

System.out.println(n+" is odd");

} else {

System.out.println(n+" is not odd");

}

}

public static void firstRoll(int a, int b) {

int sum = a+b;

if(sum==2||sum==3||sum==12) {

System.out.println("rap out");

}

else if(sum==7||sum==11) {

System.out.println("im");

}

else {

System.out.println("keep rolling");

}

}

public static String waterState(double d) {

if(d<=32) {

return "solid";

}

else if(d>32&& d<212) {

return "liquid";

}

else {

return "gas (steam)";

}

}

public static void printArea(double w, double h) {

System.out.printf("Area is %.1f for a rectangle of width %.1f and height %.1f ", w*h,w,h);

}

public static void stockSale(String name, int n, double price) {

System.out.printf("Sold %d shares of %s at $%.2f for $%.2f",n,name,price, price*n);

}

public static double determineTax(double amount, double percentage) {

return amount*percentage;

}

public static void testScore(int score) {

if(score>100 || score<0) {

System.out.println("Invalid");

} else {

char grade;

if(score>=90 && score<100) {

grade='A';

} else if(score>=80 && score<90) {

grade = 'B';

} else if(score>=70 && score<80) {

grade = 'C';

} else if(score>=60 && score<70) {

grade = 'D';

} else {

grade ='F';

}

System.out.println(score+" has a grade "+grade);

}

}

public static void main(String [] args)

{

// Sample method getting called. Just reports the String sent to the method

// by printing "Received: " followed by the String.

sampleMethod("hello");

sampleMethod("there");

// For the received integer value, report whether or not it is odd.

// It should work for any integer value (not just those used below).

// It reports a message of the format "16 is not odd" or "1 is odd".

reportOdd(3);

reportOdd(100);

reportOdd(77);

// Assume that the two values represent the width and height of

// a rectangle. Report the area of the rectangle with a line of

// the form "Area is 100.0 for a rectangle of width 10.0 and height 10.0".

// Print one digit after the decimal point.

printArea(10.00, 10.0);

printArea(3.555, 19.4);

printArea(600.5, 1.1);

// Assume that the values sent to stockSale are a String representing

// the name of the company, an integer representing the number of shares

// of the stock that were sold, and the price the stock was sold at.

// Report the information about the sale in a line of output of this

// format:

//

// Sold 50 shares of IBM at $35.50 for $1,775.00

//

stockSale("IBM", 50, 35.5);

stockSale("Apple", 100, 440.25);

stockSale("GM", 25, 12.75);

// Write the method determineTax. The method is sent the amount of the

// purchase and the tax rate. It returns a double value that is the

// amount of the tax.

// Note that the method determineTax does not print anything -- it

// just returns the value.

double m1 = determineTax(350.00, 0.06); // Michigan is 6%

double m2 = determineTax(79.99, 0.06);

double cal1 = determineTax(499.99, 0.0725); // California is 7.25%

System.out.printf("Here are the returned values: %.2f, %.2f, and %.2f ", m1, m2, cal1);

// Reports the grade that corresponds to the test score supplied, according to this scale:

// A: 100-90, B: 89-80, C: 79-70, D: 69-60, E: 59-0. Report "Invalid" for any other score.

// Invalid scores should be reported like this: "110 is an invalid score".

// Valid scores should be reported like this: "70 has a grade of C".

testScore(93);

testScore(70);

testScore(110);

testScore(-3);

testScore(52);

testScore(88);

// In the gambling game of raps the gambler rolls two dice. The first roll of the dice

// has several different outcomes. If a 2, 3, or 12 is rolled then the gambler "raps out" (i.e,

// they lose). If they roll a 7 or 11 then they "win". If they roll any other number then

// they "keep rolling".

// Write a method that takes the two dice and reports one of the three possible outcomes:

// * "rap out"

// * "win"

// * "keep rolling"

//

// This method just reports the status of just the first roll.

firstRoll(2, 3);

firstRoll(6, 1);

firstRoll(4, 5);

firstRoll(4, 3);

firstRoll(6, 6);

// Write a method waterState that takes a single double value as a parameter that indicates the

// temperature of the water. Your method will return a single String indicating the state of water

// at that temperature. The three possible states are "solid", "liquid", or "gas". I.e., water is

// solid, as ice, at 32 degrees or less, it is a gas (steam) at 212 degrees and above, and it is a

// liquid between 32 and 212.

String w1 = waterState(76.5); // 76 degrees -- liquid water

System.out.println("w1 is: " + w1);

String w2 = waterState(17.7);

System.out.println("For 17 it is: " + w2);

String w3 = waterState(32.0);

System.out.println("Right at 32 is is frozen: " + w3);

System.out.println("Hot: " + waterState(250.0));

// Write a method that returns a boolean value of true or false.

// It takes three test scores, and return true or false to indicate

// if the average grade is a passing grade (i.e., return true if the average is

// 70.0 points or more).

boolean b = passes(99.0, 83.0, 90.2);

System.out.println("b is: " + b);

boolean b2 = passes(72.0, 54.5, 69.5);

System.out.println("return value of: " + b2);

System.out.println( passes(100.0, 5.0, 82.0) );

// Write a method that uses a loop to print the squares of numbers.

// The squares are reported in increments of one from the first

// number to the second number, inclusive:

squares(2, 9);

squares(100, 105);

squares(22, 30);

}

public static void squares(int a, int b) {

for(int i=a;i<=b;i++){

System.out.print(i*i+" ");

}

System.out.println();

}

public static boolean passes(double a, double b, double c) {

double avg = (a+b+c)/3;

if(avg>=70) {

return true;

}

return false;

}

}

Output:

Received: hello
Received: there
3 is odd
100 is not odd
77 is odd
Area is 100.0 for a rectangle of width 10.0 and height 10.0
Area is 69.0 for a rectangle of width 3.6 and height 19.4
Area is 660.6 for a rectangle of width 600.5 and height 1.1
Sold 50 shares of IBM at $35.50 for $1775.00Sold 100 shares of Apple at $440.25 for $44025.00Sold 25 shares of GM at $12.75 for $318.75Here are the returned values: 21.00, 4.80, and 36.25
93 has a grade A
70 has a grade C
Invalid
Invalid
52 has a grade F
88 has a grade B
keep rolling
im
keep rolling
im
rap out
w1 is: liquid
For 17 it is: solid
Right at 32 is is frozen: solid
Hot: gas (steam)
b is: true
return value of: false
false
4 9 16 25 36 49 64 81
10000 10201 10404 10609 10816 11025
484 529 576 625 676 729 784 841 900