问题
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.
回答1:
This isn't an actual answer to your question, but it is something I've been meaning to post for a while. The problem is: what to do when you're completely stuck.
One thing I learned in college way back was to try to outline the code, rather than try to concentrate on details. Producing an outline in code and keeping the code compiling and working at the same time can help you make progress as you attempt to figure out a difficult problem.
Start with something like this:
public class SquareOrRectangle {
public static void main( String... args ) {
// 1. Get user input
// 2. Determine if square
// 3. Determine if rectangle
// 4. Print perimeter
}
}
That's it. Just start with something basic that contains all the steps you think you need. Then, start to break this down further, keeping the code still compilable and running.
public class SquareOrRectangle {
public static void main( String... args ) {
// 1. Get user input
getUserInput();
// 2. Determine if square
boolean square = isSquare();
// 3. Determine if rectangle
boolean rect = isRectangle();
// 4. Print perimeter
printPerimeter();
}
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" );
}
}
The program still doesn't DO anything, but it breaks the problem into smaller steps that are easier to solve. Importantly, the program still compiles and runs. This means you can test it. This is important because you don't want to write a huge blob of code and then have to test it all at once, you want to be able to test smaller steps so that you can find errors more quickly.
If at any step you don't know how to solve that step, break it down again into small steps, and repeat until you have steps small enough that you do know how to solve them.
That's about it. Break your program into small steps. Test each step and get it working before trying to get the rest of the code working. Write tests and code in smaller blocks, don't try to write the whole program in one go.
回答2:
I think the answer by @markspace very clearly outlines the flow. Apart from that, i think your problem statement requires you to calculate the perimeter only if it is a square or rectangle. So don't forget to add that check :)
来源:https://stackoverflow.com/questions/61254530/how-do-i-use-java-functions-to-write-a-program-that-determines-whether-4-integer