This programming assignment is intended to give you lots of practice writing met
ID: 3737467 • Letter: T
Question
This programming assignment is intended to give you lots of practice writing methods.
Copy the code supplied below and paste it in to your development environment (e.g., jGRASP). Leave the main method EXACTLY as it is -- do not change anything in the main method. Write the code for the many missing methods. A description of each method is shown just above where the method is getting called. You will not only write each method, but you also need to write a full javadoc comment for each method (see the sample comment for the add method that I've completed for you, below). Once you have written all of the missing methods, and you run the main method supplied below, then it should generate the required output shown below. Your output and the shown out should match EXACTLY.
Write the methods so that the they will work for ANY reasonable input values and NOT JUST FOR THE EXACT values sent to the methods in the main method shown below. The first method, add, has already been written -- you need to write the remaining methods.
Use good, descriptive variable names inside your methods.
Write the program so that it has good style, as outlined in the style handouts in our course's D2L Resource folder in the Content area.
Read all of the comments in the code below, as the comments list more requirements for your code.
As we've done in some in-class exercises, you should comment-out most of the main method as you work on the program (just work on one method at a time -- move the comment down each time you complete a method).
USE THIS CODE TO START YOUR PROGRAM
OUTPUT
The is the EXACT output that you should get when you run your program using the "main" method written EXACTLY as shown above:
x is: 18
Price: $150.00, 6.0% tax: $9.00, total: $159.00
Price: $3,099.95, 3.0% tax: $93.00, total: $3,192.95
Spring and Invalid month
Winter
3 is Spring
12 is Winter
15 is Invalid month
333 pixels is a phone
2300 pixels is a computer
7 pixels is too small to be a phone
9000 pixels is bigger than a computer
No, is NOT an adult!
Yes, is an adult!
d is: 15
370
24
104
Averages: 15.0 and 86.5
passed: true
passed: false
Explanation / Answer
import java.text.DecimalFormat;
public class ManyMethods {
/**
* Students should write method definitions for the all of the
* methods called in this main method. The code in the main method
* should be left EXACTLY as it is.
*
* @param args the command line arguments (not used in this program)
*/
public static void main(String[] args) {
// The add method takes in two integers and returns their sum.
int x = add(3, 15);
System.out.printf("x is: %d ", x);
// Print two digits after the decimal point for monetary values and
// one digit after the decimal point for the tax rate.
totalCost(150.00, 0.06);
totalCost(3099.95, 0.03);
// Takes in a integer that represents a month of the year, where
// 1 is January, 2 is February, etc., and returns a string representing
// the SEASON that that month appears in. Use this mapping of
// months to seasons:
// 11, 12, 1, and 2 are "Winter" (all lower case except upper case 'W')
// 3, 4, and 5 are "Spring"
// 6, 7, and 8 are "Summer"
// 9 and 10 are "Fall"
// Return "Invalid month" for any other integer values
//
String season1 = getSeason(5);
String season2 = getSeason(20);
System.out.println(season1 + " and " + season2);
System.out.println(getSeason(12));
// This method is similar to the getSeason method, except that
// instead of returning a value (this method returns nothing) this
// method prints one line to the console of the format:
// "3 is Spring"
// For invalid values print a message to the console of the
// format:
// "15 is Invalid month"
reportSeasonForMonth(3);
reportSeasonForMonth(12);
reportSeasonForMonth(15);
// This method takes in an integer parameter that represents
// the width of a screen in pixels. It prints a message to the
// console of the format:
// 333 pixels is a phone
// 2300 pixels is a computer
// 7 pixels is too small to be a phone
// 9000 pixels is bigger than a computer
// The ranges are:
// Less than 100 pixels is "too small to be a phone"
// 100 to 500 pixels, inclusive, is "a phone"
// 500 to 899 pixels is "a tablet"
// 900 to 3999 pixels is "a computer"
// 4000 pixels and above is "bigger than a computer"
//
screenType(333);
screenType(2300);
screenType(7);
screenType(9000);
// The isAdult method takes in an age and returns true if they
// are 18 years old or older, and returns false otherwise.
if (isAdult(7) == true)
System.out.println("Yes, is an adult!");
else
System.out.println("No, is NOT an adult!");
if (isAdult(35))
System.out.println("Yes, is an adult!");
else
System.out.println("No, is NOT an adult!");
// The multiply method takes in two integers and returns the
// product of those two numbers.
int result = multiply(3, 5);
System.out.printf("d is: %d ", result);
System.out.println(multiply(10, 37));
System.out.println(multiply(2, multiply(3, 4)) );
System.out.println(add( multiply(10, 10), add(2, 2)));
// The getAverage method takes in three doubles and returns their
// average.
double ave1 = getAverage(14.0, 16.0, 15.0);
double ave2 = getAverage(99.9, 75.5, 84.0);
System.out.printf("Averages: %.1f and %.1f ", ave1, ave2);
// The passed method takes in three test scores as double values,
// and returns the boolean true value if they passed the tests with
// and average test score of 70.0 or greater, otherwise it returns
// the boolean false value.
boolean status = passed(100.0, 88.0, 90.0);
System.out.println("passed: " + status);
System.out.println("passed: " + passed(73.0, 70.0, 61.0));
}
// *** Add your code BELOW this line (do not change ANYTHING above this line
// This first method is done for you:
/**
* Adds two integers and returns their sum.
*
* @param num1 The first of two integers to add.
* @param num2 The second integer to add.
* @return The sum of the two integers.
*/
public static int add(int num1, int num2) {
return num1 + num2;
}
public static void totalCost(double monetaryValue, double taxrate) {
DecimalFormat df2 = new DecimalFormat(".##");
DecimalFormat df1 = new DecimalFormat(".#");
double accTax = taxrate * 100;
double tax = (monetaryValue * accTax) / 100;
double finalCost = monetaryValue + tax;
System.out.println("Price: $" + df2.format(monetaryValue) + ", " + df1.format(accTax) + "% tax: $" + df2.format(tax) + ", total: $" + df2.format(finalCost));
}
public static String getSeason(int month) {
String season;
switch(month) {
case 1:
case 2:
case 11:
case 12:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
season = "Fall";
break;
default:
season = "Invalid month";
break;
}
return season;
}
public static void reportSeasonForMonth(int month) {
switch(month) {
case 1:
case 2:
case 11:
case 12:
System.out.println(month + " is Winter");
break;
case 3:
case 4:
case 5:
System.out.println(month + " is Spring");
break;
case 6:
case 7:
case 8:
System.out.println(month + " is Summer");
break;
case 9:
case 10:
System.out.println(month + " is Fall");
break;
default:
System.out.println(month + " is Invalid month");
break;
}
}
public static void screenType(int pixels) {
if (pixels < 100) {
System.out.println(pixels + " pixels is too small to be a phone");
} else if ((pixels > 100) && (pixels < 500)) {
System.out.println(pixels + " pixels is a phone");
} else if ((pixels > 500) && (pixels < 900)) {
System.out.println(pixels + " pixels is a tablet");
} else if ((pixels > 900) && (pixels < 4000)) {
System.out.println(pixels + " pixels is a a computer");
} else if (pixels > 4000) {
System.out.println(pixels + " pixels is bigger than a computer");
}
}
public static boolean isAdult(int age) {
if(age >= 18)
return true;
else
return false;
}
public static int multiply(int num1, int num2) {
return num1*num2;
}
public static double getAverage(double num1, double num2, double num3) {
double avergNum;
avergNum = (num1 + num2 + num3) / 3;
return avergNum;
}
public static boolean passed(double num1, double num2, double num3) {
double avergNum = getAverage(num1, num2, num3);
if(avergNum >= 70.0)
return true;
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.