User input values for a Rectangle - java

I have a class where a user inputs the x and y coordinates for the top left corner of a perfect rectangle, as well as the length and width. The RectangleViewer constructor sets the rectangle as null and then sets all the variables to 0.
Here is what I have so far:
public class RectangleViewer {
private Rectangle rectangle;
private Scanner input = new Scanner(System.in);
public RectangleViewer() {
this.rectangle = null;
this.rectangle = new Rectangle(new Point2D.Double(0.0, 0.0), 0.0, 0.0);
}
public void inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
}
public void initializeRectangle() {
this.rectangle = new Rectangle(new Point2D.Double(inputRectangle());
}
}
Here is the code for the Rectangle class itself:
public class Rectangle {
private double width;
private double length;
private Point2D.Double topLeftPoint;
public Rectangle(Point2D.Double topLeftPoint, double width, double length) {
this.width = width;
this.length = length;
this.topLeftPoint = topLeftPoint;
}
How would I set it up so that the values that are put in inputRectangleParameters are assigned to a rectangle that will be created in initializeRectangle?

The method inputRectangleParameters reads values from the command line. If you wish to create the rectangle in the initializeRectangle method, the read values need to be passed to it.
This could be done by e.g. returning the values in an object.
As the Rectangle class contains according attributes for the values, I'd recommend to create a rectangle object within the input method and returning it to initialize.
public Rectangle inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
return new Rectangle(new Point2D.Double(xCoordinate,yCoordinate),inputWidth,inputLength);
}
public void initializeRectangle() {
this.rectangle = inputRectangleParameters();
}
A less clean version would be to return e.g. an array of the values with specific positions of values.
public double[] inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
return new double[]{xCoordinate, yCoordinate, inputWidth, inputLength};
}
public void initializeRectangle() {
final double[] rectangleParameters = inputRectangleParameters();
this.rectangle = new Rectangle(new Point2D.Double(rectangleParameters[0], rectangleParameters[1]), rectangleParameters[2], rectangleParameters[3]);
}

Related

Use the results from one method to calculate the price in another method

I'm a beginner.
I'm having trouble figuring out what exactly I'm doing wrong.
I have managed to get the first method working fine(calculateArea) but I don't understand why the second method isn't working(can't find symbol of area)
import java.util.Scanner;
public class PaintCalculator
{
public static void main(String[] args)
{
double length;
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length >> ");
length = keyboard.nextDouble();
System.out.print("Enter the width >> ");
width = keyboard.nextDouble();
keyboard.nextLine();
calculateArea(length, width);
calculatePrice(area);
}
public static double calculateArea (double length, double width)
{
double area;
area = length * width;
System.out.println(area);
return area;
}
public static void calculatePrice(double area)
{
double gallons = area * 350;
double price = gallons * 32;
System.out.println(price);
}
}
Sorry if this is a noob question
You have to save the returned value from calculateArea (which is area) to double variable and the pass that to calculatePrice
public static void main(String[] args)
{
double length;
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length >> ");
length = keyboard.nextDouble();
System.out.print("Enter the width >> ");
width = keyboard.nextDouble();
keyboard.nextLine();
double area = calculateArea(length, width);
calculatePrice(area);
}
This is a classic example of how java handles data within methods. The area variable is within your calculateArea method and you are trying to use it via main and feed it into calculate price. instead assign the area to a variable and pass it in
{
public static void main(String[] args)
{
double length;
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length >> ");
length = keyboard.nextDouble();
System.out.print("Enter the width >> ");
width = keyboard.nextDouble();
keyboard.nextLine();
//calculateArea(length, width);
int area = calculateArea(length, width);
calculatePrice(area);
//or just directly place it in like this
calculatePrice(calcuateArea(length, width));
}
public static double calculateArea (double length, double width)
{
double area;
area = length * width;
System.out.println(area);
return area;
}
public static void calculatePrice(double area)
{
double gallons = area * 350;
double price = gallons * 32;
System.out.println(price);
}
}

Trouble with the output

The following code creates an error for me after running it, it projects the first line "Please enter length of a rectangle." correctly, but then the next line after I input a number is "Enter the length of the rectangle: Please enter width of a rectangle." Then after I input the second time it creates an error and crashes my code. To clarify I don't mean to have those next to each other like that. Any help would be appreciated. Thanks!
import java.util.Scanner;
public class AreaRectangle {
public static void main(String[] args) {
double length, // The rectangle's length
width, // The rectangle's width
area; // The rectangle's area
// Get the rectangle's length from the user.
length = getLength();
System.out.print("Enter the length of " +
"the rectangle: ");
// Get the rectangle's width from the user.
width = getWidth();
System.out.print("Enter the width of " +
"the rectangle: ");
// Get the rectangle's area.
area = getArea(length, width);
System.out.print("The area of the " +
"rectangle is: ");
// Display the rectangle data.
displayData(length, width, area);
System.out.print("Enter the length of " +
"the rectangle: ");
}
public static double getLength() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter length of a rectangle.");
double length = keyboard.nextDouble();
System.out.println("The length of the rectangle is " + length);
return length;
}
public static double getWidth() {
Scanner keyboard = new Scanner(System.in);
double width;
System.out.println("Please enter width of a rectangle.");
width = keyboard.nextDouble();
System.out.println("The width of the rectangle is " + width);
return width;
}
public static double getArea(double length, double width) {
double area = length * width;
System.out.println("The area of the rectangle is " + area);
return area;
}
public static double displayData(double length, double width, double
area) {
System.out.println("The length of the rectangle is: \t" + length);
System.out.println("The width of the rectangle is: \t" + width);
System.out.println("The area of the rectangle is: \t" + area);
}
}
Remove all the prompts from main(). The proper prompts are inside the other methods.
In each method that you open a Scanner object,
don't forget to close it just before the return statement: keyboard.close();.
You forgot to return value from the getLength function.
Just add a return statement on it and it will work.
you have declared:
public static double displayData(....)
so you MUST return a double value or change your function declaration to
public static void displayData
You have also another problem with your main function: you can delete all the println on the main scope. You can also remove the println inside getArea function. your code will be like:
import java.util.Scanner;
public class AreaRectangle {
public static void main(String[] args) {
double length, // The rectangle's length
width, // The rectangle's width
area; // The rectangle's area
// Get the rectangle's length from the user.
length = getLength();
// Get the rectangle's width from the user.
width = getWidth();
// Get the rectangle's area.
area = getArea(length, width);
// Display the rectangle data.
displayData(length, width, area);
}
public static double getLength() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter length of a rectangle.");
double length = keyboard.nextDouble();
System.out.println("The length of the rectangle is " + length);
return length;
}
public static double getWidth() {
Scanner keyboard = new Scanner(System.in);
double width;
System.out.println("Please enter width of a rectangle.");
width = keyboard.nextDouble();
System.out.println("The width of the rectangle is " + width);
return width;
}
public static double getArea(double length, double width) {
double area = length * width;
return area;
}
public static void displayData(double length, double width, double
area) {
System.out.println("The length of the rectangle is: \t" + length);
System.out.println("The width of the rectangle is: \t" + width);
System.out.println("The area of the rectangle is: \t" + area);
}
}
and the output of your console will be ok for your purpose.
Here is the code for you. Take a look how it is implemented and try adding some new features like parameter or diagonal of the rectangle. It isn't that hard.
public class Main {
private static double lengthOfRectangle;
private static double widthOfRectangle;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input the length of the rectangle: \n");
lengthOfRectangle = scanner.nextDouble();
System.out.println("Input the length of the rectangle: \n");
widthOfRectangle = scanner.nextDouble();
System.out.printf("Length: %s - Width: %s - Area of your rectangle is %s", lengthOfRectangle,widthOfRectangle,lengthOfRectangle*widthOfRectangle);
}
}

Finding The Area Of A Triangle

I have a question that I would like to ask you. In my book for java programming, it asks me to write a program that finds the area of a triangle given 3 points. I tried many ways but I could never get the right answer. Can you please give me a solution to this problem. Thanks! Here is the question:
Here is my code:
import java.util.Scanner;
public class shw2point15 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
double s = ((x1 + y1) + (x2 + y2) + (x3 + y3)) / 2;
double area = Math.sqrt(s * (s - (x1 - y1)) * (s - (x2 - y2)) * (s - (x3 - y3)));
System.out.println("The area of the triangle is " + area);
}
}
The reason you're not getting a correct answer is because you are not finding the sides correctly. However, after finding the side length you can get the answer. Here is what I did:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
//Store the values in an array.
double[] xCoordinates = new double[3];
double[] yCoordinates = new double[3];
double[] sides = new double[3];
// Input the values into the array
xCoordinates[0] = input.nextDouble();
yCoordinates[0] = input.nextDouble();
xCoordinates[1] = input.nextDouble();
yCoordinates[1] = input.nextDouble();
xCoordinates[2] = input.nextDouble();
yCoordinates[2] = input.nextDouble();
// Find the side length from the input. There probably are better ways to do this.
sides[0] = Math.sqrt(Math.pow(xCoordinates[0]-xCoordinates[1], 2)+Math.pow(yCoordinates[0]-yCoordinates[1], 2));
sides[1] = Math.sqrt(Math.pow(xCoordinates[1]-xCoordinates[2], 2)+Math.pow(yCoordinates[1]-yCoordinates[2], 2));
sides[2] = Math.sqrt(Math.pow(xCoordinates[2]-xCoordinates[0], 2)+Math.pow(yCoordinates[2]-yCoordinates[0], 2));
// Find s from the sides
double s = ( sides[0]+sides[1]+sides[2] )/2;
// Find the area.
double area = Math.sqrt(s*( s-sides[0] )*( s-sides[1] )*( s-sides[2] ));
// Print the area
System.out.println("The area of the triangle is "+area);
// Output~~~~~~~~~~~~~~~
//Enter three points for a triangle:
// 1.5
// -3.4
// 4.6
// 5
// 9.5
// -3.4
// The area of the triangle is 33.600000000000016
}'

Loop will not end. How do i make it exit?

Got scolded earlier for not specify the question. Promise to do better this time.
For some reason, the loop doesn't stop when i enter 0, instead it continues to print out the main method again. How do i fix this? I couldn't find out what is wrong with it, since everything else runs smoothly.
Also, I want to use Switch instead of If, but i keep getting Duplicate Variable error. Why?
import java.util.Scanner;
public class FindArea
{
// side is only input provided to cube. Area of the cube is returned
public static double cube (double side)
{
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere (double radius)
{
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder (double radius, double height)
{
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut (double outerR, double innerR)
{
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
do
{
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
} while (n != 0);
System.exit(1);
}
}
It prints the output one more time because the comparison is not made until the end of the Do-While loop.
It checks for all cases of n other than 0, and prints the output no matter what.
I think what you're looking for is more so:
n = input.nextInt();
if(n!=0){
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
}
note the n!=0
Additionally look into the use of switch-case, once you learn them these comparisons will not only be faster, but very easy to comprehend
You can try this with your switch case -
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
else{
switch(n){
case 0:{
System.out.println("You enter 0 to exit");
break;
}
case 1:{
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
}
case 2:{
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
break;
}
case 3: {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
}
case 4:{
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
}
}
}
}
i have done this using switch case. Very simple as well
public class FindArea {
// side is only input provided to cube. Area of the cube is returned
public static double cube(double side) {
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere(double radius) {
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder(double radius, double height) {
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut(double outerR, double innerR) {
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut, Or enter 0 to exit ");
int n = 0;
double side = 0;
double Area = 0;
double radius = 0;
double height = 0;
n = input.nextInt();
switch (n) {
case 0:
System.out.println("Exit");
System.exit(1);
break;
case 1:
System.out.print("Enter side measurement of cube: ");
side = input.nextDouble();
Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
case 2:
System.out.print("Enter radius measurement of sphere: ");
radius = input.nextDouble();
Area = sphere(radius);
System.out.println("The area of the sphere is: " + Area);
break;
case 3:
System.out.print("Enter radius measurement of cylinder: ");
radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
height = input.nextDouble();
Area = cylinder(radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
case 4:
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
Area = doughnut(outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
default:
System.out.println("--------");
System.out.println("Invalid option selected");
System.out.println("--------");
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
break;
}
}
}
Good Luck.

A java program to calculate the perimeter of a square,circle,rectangle and Triangle [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I was given an assignment to perform method overloading in inheritence in java by designing a program that calculates the perimeter of different shapes, i designed code as shown below but when i try to compile, there are errors.
import java.io.*;
import java.util.*;
public class Perimeter {
public double getperimeter(int constant,double pi,double radius){
return(constant*pi*radius);
}
public double getperimeter(int sconstant,double length){
return(sconstant*length);
}
public double getperimeter(int rconstant,double rlength,double widith){
return(rconstant*(rlength+widith));
}
public double getperimeter(double base,double height,double hypotenuse){
return(base+height+hypotenuse);
}
public void main (String args []){
final double pi=22/7;
final int constant=2;
double raduius;
final int sconstant=4;
double length;
final int rconstant=2;
double rlength;
double widith;
double base;
double height;
double hypotenuse;
Perimeter g= new Perimeter();
Scanner s=new scanner(System.in);
System.out.println("Enter The Radius");
g.radius=s.nextDouble();
System.out.println("Enter The Square Length");
g.lenght=s.nextInt();
System.out.println("Enter The Rectangle Lenght");
g.rlength=s.nextInt();
System.out.println("Enter The Rectangle widith");
g.widith=s.nextInt();
System.out.println("Enter The Triangle Base");
g.base=s.nextInt();
System.out.println("Enter The Triangle height");
g.height=s.nextInt();
System.out.println("Enter The Triangle hypotenuse");
g.hypotenuse=s.nextInt();
System.out.println("Perimeter = " + g.getperimeter(constant,pi,radius));
System.out.println("Perimeter = " + g.getperimeter(sconstant,length));
System.out.println("Perimeter = " + g.getperimeter(rconstant,rlength,widith));
System.out.println("Perimeter = " + g.getperimeter(base,height,hypotenuse));
This is a "classic" overriding problem that academics love. (Others involve animals or vehicles.)
Start with a Shape interface:
public interface Shape {
double getPerimeter();
}
Then have subclasses implement it, each in their own way:
public class Rectangle implements Shape {
private double height;
private double width;
public Rectangle(double w, double h) {
this.width = w;
this.height = h;
}
public double getPerimeter() { return 2.0*(this.width + this.height); }
}
public class Circle implements Shape {
private double radius;
public Circle(double r) {
this.radius = r;
}
public double getPerimeter() { return 2.0*Math.PI*this.radius; }
}
You'll be able to do things like this:
List<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Rectangle(1.0, 2.0));
shapes.add(new Circle(10.0));
for (Shape shape : shapes) {
System.out.println(shape.getPerimeter()); // every Shape will do it their own way
}
Add a new Shape implementation and your original code still just works. It's the essence of polymorphism and dynamic binding.
You are defining two methods with same signature within a single class. This is an error. Your methods :
double getperimeter(int constant,double pi,double radius);
double getperimeter(int rconstant,double rlength,double widith);
Also your main method must be declared as static
Hi you should have a the class Perimeter in a different file.
I see missing brackets closing the class Perimeter.
Also like Xavier mentioned you have two methods with the same signature.
You are also reading values from Scanner into Perimeter instance variables and then passing the uninitialized values to the Perimeter methods.
So i would separate the Perimeter class and in the main read the values to local variables and pass them to Perimeter methods.
public void main (String args []){
final double pi=22/7;
final int constant=2;
double raduius;
final int sconstant=4;
double length;
final int rconstant=2;
double rlength;
double widith;
double base;
double height;
double hypotenuse;
Perimeter g= new Perimeter();
Scanner s=new scanner(System.in);
System.out.println("Enter The Radius");
radius=s.nextDouble();
System.out.println("Enter The Square Length");
lenght=s.nextInt();
System.out.println("Enter The Rectangle Lenght");
rlength=s.nextInt();
System.out.println("Enter The Rectangle widith");
widith=s.nextInt();
System.out.println("Enter The Triangle Base");
base=s.nextInt();
System.out.println("Enter The Triangle height");
height=s.nextInt();
System.out.println("Enter The Triangle hypotenuse");
hypotenuse=s.nextInt();
System.out.println("Perimeter = " + g.getperimeter(constant,pi,radius));
System.out.println("Perimeter = " + g.getperimeter(sconstant,length));
System.out.println("Perimeter = " + g.getperimeter(rconstant,rlength,widith));
System.out.println("Perimeter = " + g.getperimeter(base,height,hypotenuse));
You are still unfamiliar with java it seems, hence start help:
The main method is static, that is executed first.
You are using overloaded methods getperimeter, where you can easily mix up int and double. Maybe pick a unique name. Java convention is to use funny camel case: getPerimeter.
import java.util.*;
public class Perimeter {
public static void main(String args[]) {
new Perimeter().execute();
}
public double getperimeter(int constant, double pi, double radius) {
return (constant * pi * radius);
}
public double getperimeter(int sconstant, double length) {
return (sconstant * length);
}
public double getperimeterRLenghtWidith(int rconstant, double rlength, double widith) {
return (rconstant * (rlength + widith));
}
public double getperimeter(double base, double height, double hypotenuse) {
return (base + height + hypotenuse);
}
private void execute() {
final double pi = Math.PI; //22 / 7;
final int constant = 2;
double radius;
final int sconstant = 4;
double length;
final int rconstant = 2;
double rlength;
double widith;
double base;
double height;
double hypotenuse;
Scanner s = new Scanner(System.in);
System.out.println("Enter The Radius");
radius = s.nextDouble();
System.out.println("Enter The Square Length");
length = s.nextInt();
System.out.println("Enter The Rectangle Lenght");
rlength = s.nextInt();
System.out.println("Ener The Rectangle widith");
widith = s.nextInt();
System.out.println("Enter The Triangle Base");
base = s.nextInt();
System.out.println("Enter The Triangle height");
height = s.nextInt();
System.out.println("Enter The Triangle hypotenuse");
hypotenuse = s.nextInt();
System.out.println("Perimeter = " + getperimeter(constant, pi, radius));
System.out.println("Perimeter = " + getperimeter(sconstant, length));
System.out.println("Perimeter = " + getperimeterRLenghtWidith(rconstant, rlength, widith));
System.out.println("Perimeter = " + getperimeter(base, height, hypotenuse));
}
}

Categories