-3

I'm new to java and we're using Eclipse in college.

I need to create a class (named Rectangle) that contains two variables: double dblLargeur, dblHauteur (stands for height and width in french)

The class should also contain two methods: public double getSurface() - returns the surface of the rectangle and main() - to test the class

Can you show me the easiest way to code this program in java?

madpitbull
  • 15
  • 1
  • 9
  • Possible duplicate of [How can I read input from the console using the Scanner class in Java?](http://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) – user7 Jan 25 '16 at 04:08
  • Please be more specific with the question. – prakashchhetri Jan 25 '16 at 04:14

2 Answers2

2

Scanners work to help get user input.

To get a string,

Scanner scan = new Scanner(System.in);
String myStr = scan.nextLine();

Integers

Scanner scan = new Scanner(System.in);
int n = scan.nextInt();

Your class is just using a scanner.

public Class Rectangle{
    private double dblLargeur;

    private double dblHauteur;
    //create the variables

    public double getSurface(){
         return dblLargeur * dblHauteur;
    }
    //your method



    public static void Main(String[] args){
        Scanner scan = new Scanner(System.in);
        dblLargeur = scan.nextDouble();
        dblHauteur = scan.nextDouble();
        System.out.println(getSurface());

    }
}

Notice how we use the nextDouble() to set the width and height.

intboolstring
  • 6,200
  • 4
  • 25
  • 40
-1

You can do it like this -->

public class Rectangle {
    double height, width;
    public Rectangle(double height, double width){
        this.height = height;
        this.width = width;
    }
    double getSurface(){
        return height * width;
    }
    public static void main (String[] args){
        // do practice here
    }
}
Tristan
  • 31
  • 7