Lesson 25 of 34

Classes and Objects

10 min 30 XP

Java is object-oriented. A class is a blueprint with fields (data) and methods (actions). We create objects from it with new, and each object has its own values.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

A file can have only one public class. Write your other classes above Main without the word public.

Challenge

+30 XP

Write a class Rectangle with fields length and width and methods area() and perimeter(). In main(), read length and width and print: Input: 5 3 Output: Area: 15 Perimeter: 16

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.