1.1.computearea.gray

1.1.computearea.gray - It’s Your Turn! Now let’s put it...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: It’s Your Turn! Now let’s put it all together: Write a Java program called ComputeArea, which asks the user for the width and height of a rectangle and computes and prints the area of the rectangle. Step 1: Program Skeleton // do we need any libraries? public class ComputeArea { public static void main(String args) { } } CSE 201 – Elementary Computer Programming 97 CSE 201 – Elementary Computer Programming 98 •This is the general skeleton for all Java programs. •The name of our program is ComputeArea 97 98 Step 2: Create Scanner import java.util.Scanner; public class ComputeArea { public static void main(String args) { Scanner keyboard = new Scanner(System.in); Step 3: Input Width import java.util.Scanner; public class ComputeArea { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print( “Please enter the width of the rectangle: "); int width = keyboard.nextInt(); } } CSE 201 – Elementary Computer Programming 99 } } CSE 201 – Elementary Computer Programming 100 •Because we are getting user input, we must import the java.util.Scanner class. •We then are able to create a Scanner named keyboard. •Now, we prompt the user for the width of the rectangle. •Notice the use of print. This allows the user to enter the width on the same line as the prompt. •Then, we store the user’s input in our variable width. 99 100 Step 4: Input Height import java.util.Scanner; public class ComputeArea { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print( “Please enter the width of the rectangle: "); int width = keyboard.nextInt(); System.out.print( “Please enter the height of the rectangle: "); int height = keyboard.nextInt(); } } CSE 201 – Elementary Computer Programming 101 Step 5: Compute Area import java.util.Scanner; public class ComputeArea { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print( “Please enter the width of the rectangle: "); int width = keyboard.nextInt(); System.out.print( “Please enter the height of the rectangle: "); int height = keyboard.nextInt(); int area = width * height; } } CSE 201 – Elementary Computer Programming 102 •Once again, we prompt the user. This time, we ask for the height of the rectangle. •Notice the use of print again. •Now, we store the input in the variable height. •This is our calculation step. •We store the output in the variable area. 101 102 Step 6: Output Area import java.util.Scanner; public class ComputeArea { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print( "Please enter the width of the rectangle: "); int width = keyboard.nextInt(); System.out.print( "Please enter the height of the rectangle: "); int height = keyboard.nextInt(); int area = width * height; System.out.println("The area is " + area); } } CSE 201 – Elementary Computer Programming 103 Complete ComputeArea import java.util.Scanner; public class ComputeArea { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print( "Please enter the width of the rectangle: "); int width = keyboard.nextInt(); System.out.print( "Please enter the height of the rectangle: "); int height = keyboard.nextInt(); int area = width * height; System.out.println("The area is " + area); } } CSE 201 – Elementary Computer Programming 104 • • Finally, we have our output to the screen. Based on our input and calculations, this will print the area of the rectangle. 103 104 ...
View Full Document

This note was uploaded on 10/07/2009 for the course CSE 201 taught by Professor All during the Spring '09 term at Ohio State.

Ask a homework question - tutors are online