Download presentation
Presentation is loading. Please wait.
Published byCaroline Wood Modified over 6 years ago
1
Program: Shape Area Print the area for various shape types using the respective formulas
2
Program details: getArea()
public double getArea() This is a getter (or accessor) method that returns the area of a shape based on its current dimensions. Since it is a getter, the method body must have a return statement (most likely at the end) that returns the requested values when the method is invoked. E.g public double getArea(){ return side1 * side1; }
3
Program details: setSide()
public void setSide(int newSide) This is a setter(or mutator) method that sets the side property of a square Since it is a setter, the method does not return a value, as such the method is declared void and the body does not contain a return statement E.g. public void setSide(int newSide){ side = newSide; }
4
Program details: Math.PI
The Math.PI constant is a value provided by Java to giv us the value of pi ( ) This means you don’t have to try and remember the value of pi, and you will always have the same value every time you use it E.g. Scanner keyboard = new Scanner(System.in); // Create a scanner object to get info from the keyboard double radius = keyboard.getDouble(); // Get the user to enter a number for the radius int circleArea = Math.PI * radius * radius; // Calculate the area of a circle using the entered value
5
Program details: JOptionPane
JOptionPane is a class that allows us to get input and output from the user using a Graphical User Interface (GUI) The details of the class are available on Oracle’s official Java documentation: showInputDialog() This is a method of the JOptionPane class that allow us to get input from the user using a GUI The method will return a String (a series of characters) even if the user enters a number E.g. String userInput = JOptionPane.showInputDialog(“Enter some information”): // Get data from user System.out.println(userInput); // Print information to the screen
6
Program details: parseInt()
This is a method of the Integer class that will convert a String representation of digits into an integer This method can be useful when using the showInputDialog() method of the JOptionPane class, because showInputDialog() always returns a string, even if the user enters numbers E.g. String userInput = JOptionPane.showInputDialog(“Enter a number for our calculation”); int number = Integer.parseInt(userInput); System.out.println( “Square of “ + number + “ is “ + (number * number));
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.