Written in Java (Object Oriented). Must run in NetBeans. Method main() should: c
ID: 3743508 • Letter: W
Question
Written in Java (Object Oriented). Must run in NetBeans.
Method main() should:
comparison();
forLoop();
whileSwitch();
Write method comparison() to do the following:
Return type of void
Declare as static
Empty parameter list
Instantiate and instance of class Scanner, pass as an argument to the constructor the value System.in
Declare two variables of primitive data type int representing two numbers
Using method System.out.println(), prompt the user to enter an integer
Store the value in one of the variables of primitive data type int by setting it equal to method nextInt() in class Scanner
Using method System.out.println(), prompt the user to enter a second integer
Store the value in the second variable of primitive data type int by setting it equal to method nextInt() in class Scanner
Equal
Not equal
Less than
Greater than
Less than or equal to
Greater than or equal to
Output to the console the results of each comparison
Write method forLoop() to do the following:
Return type of void
Declare as static
Empty parameter list
amount = 0;
principle;
rate;
time;
ci;
t = 1;
Instantiate and instance of class Scanner, pass as an argument to the constructor the value System.in
Using method System.out.println(), prompt the user to enter the initial principle investment
Store the value in the variable principle by setting it equal to method nextDouble() in class Scanner
Using method System.out.println(), prompt the user to enter the interest rate
Store the value in the variable rate by setting it equal to method nextDouble() in class Scanner
Using method System.out.println(), prompt the user to enter the number of years of the investment
Store the value in the variable time by setting it equal to method nextDouble() in class Scanner
Update the value of variable rate by setting it equal to the calculation (1 + rate / 100) to generate a percentage value
Loop for the number of years stored in the variable time
Set variable t equal (t * rate)
Update variable amount setting it equal to the calculation (principle * t)
Output to the user the amount
Calculate the compounded interest, setting variable ci equal to the calculation (amount – principle)
Output to the user their compounded interest
Write method whileSwitch() to do the following:
Return type of void
Declare as static
Empty parameter list
day
rain
Output to the console “Sunday we got 1 inch of rain”
Update variable rain to equal (rain + 1)
Output to the console “Monday we got 2 inches of rain”
Update variable rain to equal (rain + 2)
Output to the console “Tuesday we got 3 inches of rain”
Update variable rain to equal (rain + 3)
Output to the console “Wednesday we got 4 inches of rain”
Update variable rain to equal (rain + 4)
Output to the console “Thursday we got 5 inches of rain”
Update variable rain to equal (rain + 5)
Output to the console “Friday we got 6 inches of rain”
Update variable rain to equal (rain + 6)
Output to the console “Saturday we got 7 inch of rain”
Update variable rain to equal (rain + 7)
Output to the console that the value is not valid
Increment the looping variable day by 1
Output to the console how much rain was received in the past week
Written in Java (Object Oriented). Must run in NetBeans.
Method main() should:
In the main method call the following methods:comparison();
forLoop();
whileSwitch();
Write method comparison() to do the following:
Return type of void
Declare as static
Empty parameter list
Instantiate and instance of class Scanner, pass as an argument to the constructor the value System.in
Declare two variables of primitive data type int representing two numbers
Using method System.out.println(), prompt the user to enter an integer
Store the value in one of the variables of primitive data type int by setting it equal to method nextInt() in class Scanner
Using method System.out.println(), prompt the user to enter a second integer
Store the value in the second variable of primitive data type int by setting it equal to method nextInt() in class Scanner
Using the comparison operators, determine if the two variables of primitive data type int areEqual
Not equal
Less than
Greater than
Less than or equal to
Greater than or equal to
Output to the console the results of each comparison
Write method forLoop() to do the following:
Return type of void
Declare as static
Empty parameter list
Declare the following variables all of primitive data type doubleamount = 0;
principle;
rate;
time;
ci;
t = 1;
Instantiate and instance of class Scanner, pass as an argument to the constructor the value System.in
Using method System.out.println(), prompt the user to enter the initial principle investment
Store the value in the variable principle by setting it equal to method nextDouble() in class Scanner
Using method System.out.println(), prompt the user to enter the interest rate
Store the value in the variable rate by setting it equal to method nextDouble() in class Scanner
Using method System.out.println(), prompt the user to enter the number of years of the investment
Store the value in the variable time by setting it equal to method nextDouble() in class Scanner
Update the value of variable rate by setting it equal to the calculation (1 + rate / 100) to generate a percentage value
In a for loopLoop for the number of years stored in the variable time
Set variable t equal (t * rate)
Update variable amount setting it equal to the calculation (principle * t)
Output to the user the amount
Calculate the compounded interest, setting variable ci equal to the calculation (amount – principle)
Output to the user their compounded interest
Write method whileSwitch() to do the following:
Return type of void
Declare as static
Empty parameter list
Declare the following variables all of primitive data type intday
rain
In a while loop that loops for seven daysIn a switch that evaluates the variable dayIf case 0Output to the console “Sunday we got 1 inch of rain”
Update variable rain to equal (rain + 1)
If case 1Output to the console “Monday we got 2 inches of rain”
Update variable rain to equal (rain + 2)
If case 2Output to the console “Tuesday we got 3 inches of rain”
Update variable rain to equal (rain + 3)
If case 3Output to the console “Wednesday we got 4 inches of rain”
Update variable rain to equal (rain + 4)
If case 4Output to the console “Thursday we got 5 inches of rain”
Update variable rain to equal (rain + 5)
If case 5Output to the console “Friday we got 6 inches of rain”
Update variable rain to equal (rain + 6)
If case 6Output to the console “Saturday we got 7 inch of rain”
Update variable rain to equal (rain + 7)
Default caseOutput to the console that the value is not valid
Increment the looping variable day by 1
Output to the console how much rain was received in the past week
Explanation / Answer
import java.util.Scanner;
class Main {
public static void comparison()
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
a = sc.nextInt();
System.out.print("Enter another number: ");
b = sc.nextInt();
System.out.println("Equal: " + (a == b));
System.out.println("Not equal: " + (a != b));
System.out.println("Less than: " + (a < b));
System.out.println("Greater than: " + (a > b));
System.out.println("Less than or equal to: " + (a <= b));
System.out.println("Greater than or equal to: " + (a >= b));
}
public static void forLoop()
{
double amount = 0, principle, rate, time, ci, t = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter initial principle investment: ");
principle = sc.nextDouble();
System.out.print("Enter initial interest rate: ");
rate = sc.nextDouble();
System.out.print("Enter the number of years of investment: ");
time = sc.nextDouble();
rate = (1 + rate / 100.0);
for(int i=1; i<= time; i++)
{
t = t*rate;
}
amount = principle * t;
System.out.println("Amount = "+amount);
ci = amount - principle;
System.out.println("Compound Interest = "+ci);
}
public static void whileSwitch()
{
int day=0, rain=0;
while(day < 7)
{
switch(day)
{
case 0:
System.out.println("Sunday we got 1 inche of rain");
rain = rain + 1;
break;
case 1:
System.out.println("Monday we got 2 inches of rain");
rain = rain + 2;
break;
case 2:
System.out.println("Tuesday we got 3 inches of rain");
rain = rain + 3;
break;
case 3:
System.out.println("Wednesday we got 4 inches of rain");
rain = rain + 4;
break;
case 4:
System.out.println("Thursday we got 5 inches of rain");
rain = rain + 5;
break;
case 5:
System.out.println("Friday we got 6 inches of rain");
rain = rain + 6;
break;
case 6:
System.out.println("Saturday we got 7 inches of rain");
rain = rain + 7;
break;
default:
System.out.println("Not valid");
}
day++;
}
System.out.println("Total rains received in the past week are "+rain);
}
public static void main(String[] args) {
comparison();
forLoop();
whileSwitch();
}
}
/* SAMPLE OUTPUT
Enter a number: 5
Enter another number: 2
Equal: false
Not equal: true
Less than: false
Greater than: true
Less than or equal to: false
Greater than or equal to: true
Enter initial principle investment: 1000
Enter initial interest rate: 20
Enter the number of years of investment: 3
Amount = 1728.0
Compound Interest = 728.0
Sunday we got 1 inche of rain
Monday we got 2 inches of rain
Tuesday we got 3 inches of rain
Wednesday we got 4 inches of rain
Thursday we got 5 inches of rain
Friday we got 6 inches of rain
Saturday we got 7 inches of rain
Total rains received in the past week are 28
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.