Reputation: 79
I'm fairly new to coding. I'm not sure how to move the input values across the methods. I tried different ways but it always gave me an error. Also I'm curious as to how you would return multiple values. Like lets say instead of having getLength() and getWidth() there would be one called getInput() were both length and width would be returned.
import java.util.Scanner; // Needed for the Scanner class
/**
This program calculates the area of a rectangle.
*/
public class Rectangle
{
//main method
public static void main(String[] args)
{
float length = 0;
float width = 0;
float area = 0;
getLength();
getWidth();
calcArea(length, width);
displayData(area);
}
//method 2
public static float getLength()
{
// Create a Scanner object to read from the keyboard.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Length: ");
float length = keyboard.nextFloat();
return length;
}
public static float getWidth()
{
// Create a Scanner object to read from the keyboard.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Width: ");
float width = keyboard.nextFloat();
return width;
}
public static float calcArea(float length, float width)
{
float area = length*width;
System.out.print("\nxxxx "+area);
return area;
}
public static void displayData(float area)
{
System.out.print("\nThe area of the rectangle is "+area);
}
}
Upvotes: 1
Views: 138
Reputation: 700
Your problem comes from the fact that non-void methods will return values. If you don't assign those values to any variables, Java won't do anything with them.
float length = 0;
float width = 0;
float area = 0;
// Values are retrieved, but must be assigned to variables
length = getLength();
width = getWidth();
area = calcArea(length, width);
I urge to you try something a little more intuitive. I understand that you're new to coding, but try messing around with this and see where it gets you. OOP is a core component of Java and it's generally good practice to use it whenever possible.
Rectangle.java:
public class Rectangle
{
private float length;
private float width;
public Rectangle(float length, float width)
{
this.length = length;
this.width = width;
}
public float getArea()
{
return length * width;
}
public float getLength()
{
return length;
}
public float getWidth()
{
return width;
}
}
Main class:
public static void main(String[] args)
{
// Only open one stream at a time
// Opening multiple without closing the previous may cause some problems in the future
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Length: ");
float length = keyboard.nextFloat();
System.out.print("Enter Width: ");
float width = keyboard.nextFloat();
// Remember to close the stream when you are finished using it
keyboard.close();
// Create a new rectangle with the length and width that was given by the user
Rectangle rect = new Rectangle(length, width);
// Round the area to the nearest tenth of a decimal place and display results
System.out.printf("The area of the rectangle is %.1f", rect.getArea());
}
Upvotes: 2
Reputation: 2349
This should help:
length = getLength();
width = getWidth();
area = calcArea(length, width);
displayData(area);
Upvotes: 1