A class called Employee, which models an employee with an ID, name and salary, is designed as shown in the following class diagram. The method raiseSalary (percent) increases the salary by the given percentage. Develop the Employee class and suitable main method for demonstration.
PROGRAM:
package
jo;
public
class Employee {
int
id;
String name;
double
salary;
public
Employee(int
id, String name,
double salary)
{
this.id
= id;
this.name
= name;
this.salary
= salary;
}
public
void raiseSalary(double
percentage) {
if
(percentage > 0) {
salary
+= salary * (percentage
/ 100.0);
}
}
public
String toString() {
return
"ID: " + id
+ "\nName: "
+ name + "\nSalary:
" + salary;
}
public
static void
main(String[] args)
{
Employee employee1
= new Employee(1, "John
Doe", 50000.0);
Employee employee2
= new Employee(2, "Jane
Smith", 60000.0);
System.out.println("Initial
Employee Information:");
System.out.println(employee1);
System.out.println(employee2);
// Raise the salary
of employee1 and employee2
employee1.raiseSalary(10);
employee2.raiseSalary(5);
// ✅ These lines are now inside main()
System.out.println("\nEmployee
Information After Raise:");
System.out.println(employee1);
System.out.println(employee2);
}
}
OUTPUT:
Viva answers
1) Why do we use this
keyword?
In Java, the this
keyword is used
to refer to the current object of
the class.
Uses of this
:
1. To
refer to instance variables when local variables have
the same name.
2. To
call other constructors in the same class.
3. To
pass the current object as a parameter.
Example:
public class Student {
int id;
String name;
Student(int id, String name) {
this.id = id; // this.id refers to the instance variable
this.name = name; // name is the parameter
}
}
Without this
, Java
wouldn't know if you're referring to the local variable or the instance
variable.
2) What is a Constructor?
Answer:
A constructor is a special method in Java
that is automatically called when an object is created.
·
It has the same name as the class.
·
It does not have a return type.
·
It is used to initialize objects.
Example:
public class Employee {
int id;
String name;
// Constructor
Employee(int id, String name) {
this.id = id;
this.name = name;
}
}
You can also have default constructors (no parameters) or parameterized
constructors (with parameters).
3) What is the toString()
method?
The toString()
method in
Java is used to convert an object into a
readable string format.
Why use it:
·
It is automatically called when
you print an object using System.out.println()
.
·
You can override it to display
your custom output.
Example:
public class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return "ID: " + id + ", Name: " + name;
}
}
When you do:
Student s = new Student(1, "Alice");
System.out.println(s);
It will print:
ID: 1, Name: Alice
Instead of the default like Student@1a2b3c
.
4) A class called MyPoint, which
models a 2D point with x and y coordinates, is designed as follows:
● Two instance
variables x (int) and y (int).
● A default (or
"no-arg") constructor that construct a point at the default location
of (0, 0).
● A overloaded
constructor that constructs a point with the given x and y coordinates.
● A method
setXY() to set both x and y.
● A method
getXY() which returns the x and y in a 2-element int array.
● A toString()
method that returns a string description of the instance in the format
"(x, y)".
● A method
called distance(int x, int y) that returns the distance from this point to
another point at the
given (x, y)
coordinates
● An overloaded
distance(MyPoint another) that returns the distance from this point to the
given MyPoint
instance
(called another)
● Another
overloaded distance() method that returns the distance from this point to the
origin (0,0)
Develop the
code for the class MyPoint. Also develop a JAVA program (called TestMyPoint) to
test all the
methods defined
in the class.
No comments:
Post a Comment