Reputation: 1
I am trying to create a program that contains two classes with a constructor. It must return an answer on the area and perimeter of a rectangle. I wrote the program but I can't figure out how to get it to return. I am having problems creating the second class and using the constructor.
import java.util.Scanner
public class RectangleCalc{
public static void main(String[] args) {
Rectangle myRect = new Rectangle(1.5, 2.3);
double Area;
double Perimeter;
greetUser(); // method call
userInput(); // method call
userGoodbye(); // method call
myRect.setLength(0);
myRect.setWidth(0);
Area = myRect.area();
Perimeter = myRect.perim();}
public static void greetUser(){
System.out.println("Welcome to the Rectangle Calculator");}
public static void userInput(){
System.out.println("This program will accept the user input of length and width to calculate the perimeter and area of a rectangle.");
System.out.println("Would you like to continue Y/N?");
System.out.println("Enter the width ");
System.out.println("Enter the length ");
}
public static void Results(double area, double pr, double width, double length){
Scanner input=new Scanner;
System.out.println ("The width you entered is:" + width );
System.out.println ("The length you entered is:" + length);
System.out.println ("The area of your rectangle is:" + area );
System.out.println ("The perimeter of your rectangle is:" + pr);
System.out.println ("Would you like to calculate another rectangle Y/N?");}
public static void userGoodbye(){
System.out.println ("Thank you for using the Rectangle Calculator. Goodbye!");}
Rectangle newRect = new Rectangle(10, 20);
}
class Rectangle{
public double width, length;
public double len, wid;
public void setWidth(double w) {
width = w;
}
public void setLength(double ln) {
length = ln;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public double area() {
double area;
area = length * width;
return area;
}
public double perim() {
double pr;
pr = (length * 2) + (width * 2);
return pr;
}
public Rectangle(double len, double wid) {
}
}
Upvotes: 0
Views: 294
Reputation: 5012
Firstly your question title could be a lot more descriptive.
You should follow Java convention in your Rectangle
class and define the constructor before its methods. I almost thought that you hadn't written one.
Indentation. While Java does not rely on properly indented code, humans kind of do. Try to always keep your code blocks and levels uniformly indented. Use either tabs or what's almost becoming the standard - 4 spaces, per indent level. Having your code indented incorrectly will only lead to difficulty down the line when trying to follow the flow of your program and possible cause errors due to bad assumptions as to what code block any line is actually part of.
Java variables and methods should start with a lowercase letter (pascalCase to be specific). double area
and double perimeter
. Class names are CamelCase and start with upper-case letters.
Edit: You've confused the code markup parser here too. Take a look at how your variables have been coloured in the code block of your post.
But on to your code...
Your constructor accepts two arguments but never assigns those values anywhere. They just get lost.
Your userInput()
method asks questions but doesn't try to get any answers. It just prints some text.
Your Results(...)
method (see above case issue) declares a Scanner
but doesn't use it. You probably meant it to go into the currently pointless userInput()
method.
So:
Use your defined setters in your constructor, grab some user input in your userInput()
method and if things haven't improved come back here and we can help you get farther.
Upvotes: 1
Reputation: 843
looking over your code I spotted a few issues:
import java.util.Scanner
requires a semi-colon on the end: import java.util.Scanner;
Your Rectangle class doesn't set any of its fields in the constructor. Try this:
public Rectangle(double len, double wid) {
width = wid;
length = len;
}
You have duplicate (and unnecessary) extra fields in your Rectangle class: wid and len. Only width and length are used.
Upvotes: 0
Reputation: 2692
Here
public Rectangle(double len, double wid) {
}
do this
public Rectangle(double len, double wid) {
setLength(len);
setWidth(wid);
}
That's it!
Upvotes: 2