I am a student studying an online course in computer programming. So far, I seem to be doing well throughout the Spring semester. However, I was assigned homework this week that is designed to test my knowledge of Java functions. I am supposed to write a program that asks a user to enter 4 integers, then determines whether those 4 entries form a square or a rectangle. If the 4 entries do make a square or rectangle, then the supposed program has to calculate its perimeter.
The output is supposed to adhere to the standards below:
Example 1:
Enter side 1
5
Enter side 2
3
Enter side 3
3
Enter side 4
5
Forms a rectangle with perimeter of 16
Example 2:
Enter side 1
5
Enter side 2
5
Enter side 3
5
Enter side 4
5
Forms a square with perimeter of 20
Example 3:
Enter side 1
5
Enter side 2
6
Enter side 3
7
Enter side 4
8
Does not form a rectangle or square.
I reread the assignment questions a couple of times and tried out whatever I could remember from studying all the classwork for this week. So far, I could only draft half of my work on such a program:
import java.io.*;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
getUserInput();
int a;
int b;
int c;
int d;
int perimeter;
boolean square = isSquare();
boolean rect = isRectangle;
printPerimeter();
}
System.out.println("Enter side 1: ");
a = Integer.parseInt(reader.readLine());
System.out.println("Enter side 2: ");
b = Integer.parseInt(reader.readLine());
System.out.println("Enter side 3: ");
c = Integer.parseInt(reader.readLine());
System.out.println("Enter side 4: ");
d = Integer.parseInt(reader.readLine());
if (a == c && b == d) {
System.out.println("It is a rectangle.”);
perimeter += side;
}
else if (a == b && b == c && c == d && d == a) {
System.out.println("It is a square.”);
perimeter += side;
}
else {
System.out.println("Incorrect. Try again.");
}
private void getUserInput() {
System.out.println("Getting user input");
}
private boolean isSquare() {
System.out.println("Is square?");
}
private boolean isRectangle() {
System.out.println("Is rectangle?");
}
private void printPerimeter() {
System.out.println("Printing perimeter");
}
}
Now I need to figure out how to get the code working without functions first. Then, I have to break the code out into functions. How do I start with a program that captures 4 sides of the object from the user and calculates the perimeter?
Thank you for your time and consideration.