Question: I need to create a pseudocode for the code below. package oneProgramHW4; import javax.swing.JOptionPane; public class oneProgramHW4 { //1.Write and test the following method that
I need to create a pseudocode for the code below.
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
String s = "";
//Generating given number of lines
for(int i = 1; i <= a; i++) {
//For getting five stars
for(int j=0; j<5; j++) {
//Adding *
s += "*";
}
//Adding new lines
s += " ";
}
//Displaying string
JOptionPane.showMessageDialog(null, s);
}
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.
else if(mainMenu == 5){
int a = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter number of lines to print "));
printNLines(a);
mainMenu = Integer.parseInt(JOptionPane.showInputDialog(null, menuDisplay));
}
else if (mainMenu == 6) {
System.exit(1);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
