Java bootcamp · Lab 43

Rectangle area

mediumClasses12 minLesson: Classes

Read the question, write Java on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Class Rectangle with w,h and int area(). Read w h; print area.

Examples

Example 1
Input
3 4
Output
12
Hint
  1. Simple class with fields and method.
Show correct code

Peek only after you have tried. You can still Check your own version.

import java.util.Scanner;

class Rectangle {
  int w, h;
  Rectangle(int w, int h) { this.w = w; this.h = h; }
  int area() { return w * h; }
}

public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int w = in.nextInt(), h = in.nextInt();
    System.out.println(new Rectangle(w, h).area());
  }
}
Main.javaJava 21 · javac · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.