("annual salary is: " + e1. (e1) // toString() // Test Setters and Getters It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. Write the Employee class.īelow is a test driver to test the Employee class:Įmployee e1 = new Employee(8, "Peter", "Tan", 2500) Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects. The method raiseSalary(percent) increases the salary by the given percentage. Perimeter is: 26.80 Ex: The Employee ClassĪ class called Employee, which models an employee with an ID, name and salary, is designed as shown in the following class diagram. ("perimeter is: %.2f%n", r1.getPerimeter()) Rectangle r2 = new Rectangle() // default constructor Rectangle r1 = new Rectangle(1.2f, 3.4f) Test constructors and toString() // You need to append a 'f' or 'F' to a float literal Write the Rectangle class.īelow is a test driver to test the Rectangle class: The expected output is: CircleĬircumference is: 13.82 Ex: The Rectangle ClassĪ class called Rectangle, which models a rectangle with a length and a width (in float), is designed as shown in the following class diagram. ("circumference is: %.2f%n", c1.getCircumference()) Write the Circle class.īelow is a Test Driver to test your Circle class.Ĭircle c2 = new Circle() // default constructor The final class diagram for the Circle class is as follows: Ex: Yet Another Circle ClassĪ class called Circle, which models a circle with a radius, is designed as shown in the following class diagram. Encapsulation is hiding unnecessary details from. ("Operator '+' invokes toString() too: " + c6) // '+' invokes toString() too Java Object Oriented programming concepts include Encapsulation, Abstraction, Inheritance and Polymorphism. (c6) // println() calls toString() implicitly, same as above (c5.toString()) // explicit call toString() is called implicitly when an instance is passed to println() method, for example, Try calling toString() method explicitly, just like any other method: ** Return a self-descriptive string of this instance in the form of Circle */ For example, include the following toString() methods to the Circle class: If an instance is passed to the println( anInstance) method, the toString() method of that instance will be invoked implicitly. The toString() method can be called explicitly (via instanceName.toString()) just like any other method or implicitly through println(). Method toString(): Every well-designed Java class should contain a public method called toString() that returns a description of the instance (in the return type of String).Modify ALL the constructors and setters in the Circle class to use the keyword " this". "radius" refers to the method's argument ** Sets the radius to the given value */ "radius" refers to the method's parameter This.radius = radius // "this.radius" refers to the instance variable ![]() ** Constructs a Circle instance with the given radius and default color */ Keyword " this": Instead of using variable names such as r (for radius) and c (for color) in the methods' arguments, it is better to use variable names radius (for radius) and color (for color) and use the special keyword " this" to resolve the conflict between instance variables and methods' arguments.("color is: " + c4.getColor()) // Print color via getter // You cannot do the following because setRadius() returns void, which cannot be printed ![]() ![]() ("radius is: " + c4.getRadius()) // Print radius via getter Modify the TestCircle to test these methods, e.g.,Ĭircle c4 = new Circle() // construct an instance of Circle * The Circle class models a circle with a radius and color. ![]() The source codes for Circle.java is as follows: /** Wrapping data and its related functionality into a single entity is known as a) Abstraction b) Encapsulation c) Polymorphism d) Modularity View Answer 2. Two public methods: getRadius() and getArea(), which return the radius and area of this instance, respectively.Two overloaded constructors - a default constructor with no argument, and a constructor which takes a double argument for radius.Two private instance variables: radius (of the type double) and color (of the type String), with default value of 1.0 and " red", respectively.This first exercise shall lead you through all the basic concepts in OOP.Ī class called circle is designed as shown in the following class diagram. Exercises on Classes An Introduction to Classes and Instances by Example - The Circle Class
0 Comments
Leave a Reply. |