Question: Problem Write a Java program that convert the Raptor flowchart in the assignment 1 to Java. Since the lab #3 is the first Java lab
Problem Write a Java program that convert the Raptor flowchart in the assignment 1 to Java. Since the lab #3 is the first Java lab assignment, I will guide you step-by-step how to create the program. Here is the Raptor flowchart: All the statements highlighted in blue are Java. You type them in the program. Open jGRASP, create a new Java file. Click File and then click New. You can make comments in a Java program. A comment is not a Java statement. jGRASP will not compile Java comments to check for syntax errors. Every Java comments should start with //. Write the followng comments statements //CIS 103 //Lab assignment 03 //Name: Your name Type the following statements: import java.util.*; The statement import java.util.*; will include all Java classes in a java package called util. We use one of theses classes to read the user's input from the keyboard. We create a class named lab3. After the statement import java.util.*; , type the following: public class lab3 { Every Java program starts with a class. lab3 is the programmer-defined class name. The class name lab3 will become the file name. The file extension of a java program is called java. The complete java file name will be lab3.java. The right brace ( { ) indicates the beginning of a Java class. After the brace ({ ), type the following: public static void main(String args []) { In our lab assignments, we always create a method in a class named main() because it's always the first method that executes a Java program. The right brace ( { ) indicates the beginning of the main method. We convert all the logic steps between the "Start" symbol and the "End" symbol in the Raptor flowchart to java structure. We create a Scanner object called input (input is a programmer-user defined name. Sometimes programmers name input as console.) to read the user's input from the keyboard. After the right brace { , type the following: Scanner input = new Scanner(System.in); The input Raptor symbol has to components. The first component is the prompt message "Enter the height (feet): " . The second component is a statement to get the user input from the keyboard and assign the input to a variable named height. Type the following after the statement Scanner input = new Scanner(System.in); System.out.println("Enter the height (feet) : "); double height; height = input.nextDouble( ); The statement System.out.print("Enter the height (feet) : ") displays a prompt message "Enter the height (feet) : ". The method println displays something and then moves the cursor to the next line. In Java, you must declare a variable before using it (Chapter 2 in Java book). When you declare a variable, you tell the compiler that you are going to use the variable. In the process of declaring a variable, you must specify the variable's name and its data type (double height). Use data type double for real numbers, int for integer numbers, and String for text or string. After the user enters the input, the statement input.nextDouble() reads the user's input stored in the memory. You assign the input to the variable height. You can use the statement input.nextInt( ) to read the user's input of an integer number. Type the following statements to read the user's input for the width. System.out.print("Enter the width (feet) : "); double width; width = input.nextDouble( ); You calculate the area of a rectangle. Type the following: double area; area = height * width; Again you must declare a variable before using it. The statement double area; declares a variable named area with a data type of double. As you know, a constant is a value that never changes. In Java, you assign a constant to a named constant or a constant variable. A named constant is similar to a variable, except it can be assigned a value once (Chapter 2 from the Java book). To declare a named constant in Java, you use the keyword final, followed by the data type. Type the following statement to declare a double constant named GALLONS_PER_SQUARE_FEET. final double GALLONS_PER_SQUARE_FEET = 150.0; Type the following statements to calculate the number of gallons needed to paint the wall. double gallons; gallons = area/GALLONS_PER_SQUARE_FEET; You convert the Raptor output symbol to a Java statement. Type the following statement: System.out.println("The amount of paint is " + gallons + " gallons."); The method println( ) displays something and then move the cursor to the next line. Add a left brace ( } ) to close the main. After the right brace you 've just added, add another left brace ( } ) to close the class. Next you compile the program. If you see the syntax errors, you need to fix them. Most of the errors are misspelled or case-sensitive. When you execute the program, your output should look like below: Enter the height (feet); 150 Enter the width (feet); 10 The amount of paint is 10.0 gallons. To compile the program, click Build, and then click Compile. If your program have any syntax errors, fix them, and then recompile the program. If you want to see the line numbers in your program, click View, and then click on the check box "Line Numbers". Until the program has no syntax errors, you can execute the program by clicking Build, and Run. If you don't remember how to create, compile and run a Java program, check out the link called "How to create, compile, and execute a Java program using jGRASP" under the User Guide section at the left hand-corner of the class Web page. Here is a complete Java program. //CIS 103 //Lab assignment 03 //Name: Your name import java.util.*; public class lab3 { public static void main(String args []) { Scanner input = new Scanner(System.in); System.out.print("Enter the height (feet); "); double height; height = input.nextDouble( ); System.out.print("Enter the width (feet); "); double width; width = input.nextDouble(); double area; area = height * width; final double GALLONS_PER_SQUARE_FEET = 150.0; double gallons; gallons = area/GALLONS_PER_SQUARE_FEET; System.out.println("The amount of paint is " + gallons + " gallons."); }//end main }//end class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
