-2

So this situation is a bit frustrating for me because my online teacher prefers us to just learn the lessons online by ourselves independently and everywhere I have looked I still cannot find out how to use no-args completely

The following is the program I am creating, however as I know it I cannot run it without a main, but whenever I enter a main it does not run the no-args construct and I just in general have no idea where I am messing up.

import java.util.Scanner;
public class Week07_Assignment {

    Scanner input = new Scanner(System.in);

    private double height = 1;
    private double width=1;
    private double h;
    private double w;


    public void createrectangle(){
        System.out.println("Give me both the height and width of this rectangle");
        height = heightgetter(height);
        width = heightgetter(width);
        area(height, width);
        perimeter(height, width);
    }

    public double heightgetter(double a){
        a = input.nextDouble();
        return a;

    }

    public double widthgetter(double a){
        a = input.nextDouble();
        return a;
    }

    public void area(double a, double b){
        double area = a * b;
        System.out.println("This is the area: " +area);

    }

    public void perimeter(double a, double b){
        double perimeter = 2 * (a + b);
        System.out.println("This is the area: " +perimeter);

    }
}
zenon
  • 29
  • 2
  • 2
    The `main` method is a static method, no constructor is called. If you need to call the default constructor, you have to do it explicitly. And if your professor refuses to explain things his students didn't understand, well... Not a good professor IMO. – BackSlash Feb 23 '17 at 11:28
  • if you want to run constructor of "Week07_Assignment" then you have to create object of it. – Snehal Patel Feb 23 '17 at 11:29

2 Answers2

0

Hi as @BackSlash mentioned, you could use the code below.

public static void main (String[] args)
 {
     Week07_Assignment object = new Week07_Assignment();

    object.createrectangle(); 

 }
hoChay
  • 37
  • 10
  • @zenon No worries, you could have the main method in a seperate class called program for example. That way you can seperate your logic into classes – hoChay Feb 23 '17 at 11:39
0

hoChay's answer is correct. In addition to his comment, here is one way to separate your logic into different classes.


Starting from scratch (and based on your question) you will need to create a class Rectangle which should only have methods that concern themselves with everything related to a rectangle, such as calculating the perimeter or the area.

Next you should create a class that concerns itself with reading the user's input (eventhough it's quite an overkill for your assignment), namely InputReader.

Last but not least create your Main class which will act as starting point when executing your code.

The Rectangle

package assignment_07;

public class Rectangle {
    private double height;
    private double width;

    private double area;
    private double perimeter;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public void create() {
        area(this.height, this.width);
        perimeter(this.height, this.width);
    }

    private double area(double width, double height) {
        this.area = width * height;
        return this.area;
    }

    private double perimeter(double width, double height) {
        this.perimeter = 2 * (width + height);
        return this.perimeter;
    }

    public double getPerimeter(){
        return this.perimeter;
    }

    public double getArea(){
        return this.area;
    }

    @Override
    public String toString() {
        return "Perimeter: " + this.perimeter + "\nArea: " + this.area;
    }
}

The InputReader

package assignment_07;
import java.util.Scanner;

public class InputReader {
    private Scanner scanner;

    public InputReader() {
        scanner = new Scanner(System.in);
    }

    public double readDouble(){
        return scanner.nextDouble();
    }
}

The Starting Point (Main)

import assignment_07.InputReader;
import assignment_07.Rectangle;

public class Main {
    public static void main(String[] args) {
        InputReader inputReader = new InputReader();

        // Get measurements from user
        double width = inputReader.readDouble();
        double height = inputReader.readDouble();

        // Create rectangle
        Rectangle rectangle = new Rectangle(width, height);
        rectangle.create();

        // Print
        System.out.println(rectangle);
    }
}

Note that the InputReader and Rectangle class are in a separate package, in order to keep any naming convention that is required for your assignment.

mcuenez
  • 1,579
  • 2
  • 20
  • 28
  • I don't understand what `InputReader` is for. it's just a wrapper around `Scanner` – Sharon Ben Asher Feb 23 '17 at 12:40
  • Indeed, that's its only purpose. That's why I mentioned it is an overkill - meaning it is not necessary. However, I assumed that the assignment might either be more than just the given description or used as a base for following assignments. Thus, if the `InputReader` gets extended it does not obfuscate the `Main` class. – mcuenez Feb 23 '17 at 12:58