Question: I have this homework below and below that is my code. Homework: Submission instructions: The following methods can be written and tested in one program.

I have this homework below and below that is my code.

Homework: Submission instructions: The following methods can be written and tested in one program. Note: To receive full credit, each method should be tested with at least TWO different method calls. Unless the directions specify, do not include input or output in your methods! 1. Write and test the following method that calculates and returns the area of a rectangle. public static float rectangleArea(float, float); 2. Write and test the following method that determines whether a given number is even. If it is even, return true, if it is odd, return false. public static boolean isEven(int); 3. Write and test the following method that accepts two integer parameters, i and j, and then returns the sum of the numbers from i to j. public static int sum(int, int); 4. Write and test the following method that determines accepts an integer value n and returns the sum of the first n natural numbers. public static int sumN(int); 5. Write and test the following method that accepts an integer value n and displays n lines of 5 stars each. public static void printNLines(int);

my code # having dificulties with menu number 5

package oneProgramHW4;

import javax.swing.JOptionPane;

public class oneProgramHW4 {

//1.Write and test the following method that calculates and returns the area of a rectangle.

public static float rectangleArea(double length, double width) {

double area = 0;

area = (length * width);

float result = (float) area;

return (float) result;

}

//2.Write and test the following method that determines whether a given number is even.

//If it is even, return true, if it is odd, return false.

public static boolean isEven(int num) {

if(num % 2 == 0) {

return true;

}

else {

return false;

}

}

//3.Write and test the following method that accepts two integer parameters, i and j,

//and then returns the sum of the numbers from i to j.

public static int sum(int i, int j) {

return Integer.sum(i, j);

}

//4.Write and test the following method that determines accepts an integer value n

//returns the sum of the first n natural numbers.

public static int sumN(int n) {

int sum=0;

for(int x=1;x<=n;x++)//calculating sum of first n natural numbers

sum+=x;

return sum;

}

//5. Write and test the following method that accepts an integer value n

//displays n lines of 5 stars each.

public static void printNLines(int a) {//took semicolon out

for(int i = 0 ; i <= a; i++) // print n lines

a = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter number of lines to print "));

JOptionPane.showMessageDialog(null,printNLines(a));// cant make this work

}

public static void main(String[] args) {

//test my method

//prompt user to chose from menu

//main loop

int mainMenu = 0;

String menuDisplay = ("Please chose from the following options: 1. Caculate rectangle area 2. Find if number is even 3. Return the sum of numbers 4. Returns the sum of the first n natural numbers 5. Displays lines of 5 stars each. 6. Exit ");

mainMenu = Integer.parseInt(JOptionPane.showInputDialog(null, menuDisplay));

//change selection to if else if

if(mainMenu ==1) {

double length = 0;

double width = 0;

//Prompt for length and width

length = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter length"));

width = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter width"));

// calculate rectangle area

JOptionPane.showMessageDialog(null, "The rectangle area is "+ rectangleArea(length,width));

mainMenu = Integer.parseInt(JOptionPane.showInputDialog(null, menuDisplay));

}

//Write and test the following method that determines whether a given number is even.

//If it is even, return true, if it is odd, return false.

else if(mainMenu ==2) {

//even number

int num = 0;// variable

//prompt for number

num = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter your number"));

JOptionPane.showMessageDialog(null, isEven(num));

mainMenu = Integer.parseInt(JOptionPane.showInputDialog(null, menuDisplay));

}//3.Write and test the following method that accepts two integer parameters, i and j,

//and then returns the sum of the numbers from i to j.

else if(mainMenu ==3) {// accepts and returns the sum of the numbers from i to j.

int i = 1;

int j = 1;

i = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter number one" + i));

j = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter number two" + j));

// calculate the sum of numbers

JOptionPane.showMessageDialog(null, " The sum is " + sum(i, j));

mainMenu = Integer.parseInt(JOptionPane.showInputDialog(null, menuDisplay));

}

//4.Write and test the following method that determines accepts an integer value n

//returns the sum of the first n natural numbers.

else if(mainMenu == 4) {

int n = 0;

//sum of the first n natural numbers

//sum of first 5 numbers is 55

n = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter first n number to calculate"));

JOptionPane.showMessageDialog(null, "The sum of the first " + n + " numbers is " + sumN(n));

mainMenu = Integer.parseInt(JOptionPane.showInputDialog(null, menuDisplay));

}

//5. Write and test the following method that accepts an integer value n

//displays n lines of 5 stars each.

// this is where I am stuck, I cant make Joption.pane work printing n times according to the user input.

else if(mainMenu == 5){

JOptionPane.showMessageDialog(null,printNLines(a));

}

else if(mainMenu == 6) {

System.exit(1);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!