public class Employee
{
public String name;
public double salary;
public Employee()
{
name="Shooq";
salary=0;
}
public Employee(String A,double B)
{
name=A;
salary=B;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public void raiseSalary(double percent)
{
salary=salary+(salary / percent);
}
public void printLnfo()
{
System.out.println("Employee Name:"+name+",Salary:"+salary);
}
}Creating an application and a set of objects
Write a Java application (driver class) MyProg” to create a set of objects emp1, emp2, emp3 of the class Employee. For each object, define initial values for name, and salary through the class constructor. Call the various methods of class Employee on these objects of the class. Compile and run the program to verify the output.
public class MyProg
{
public static void main(String[]args)
{
Employee emp1=new Employee("Shooq Ahmad",1000);
Employee emp2=new Employee("Amal Said",500);
Employee emp3=new Employee("noor Ali",600.5);
System.out.print(emp1.getName());
System.out.print("'s salary is:OR");
System.out.println(emp1.getSalary());
emp1.raiseSalary(10);
System.out.print("After the raise,");
System.out.print(emp1.getName());
System.out.print("'s salary is:OR");
System.out.println(emp1.getSalary());
}
}
Output:
Shooq Ahmad's salary is:OR1000.0
After the raise,Shooq Ahmad's salary is:OR1100.0
Finished executingNeed for Restricted Access to Variables
Now, add the following lines to MyProg” (inside main”):
emp3 = emp1;
emp3.salary=1000; //***
emp1.printInfo(); //print information of employee 1
Even though the above code has apparently changed the salary of ‘emp3’, you will notice that the salary of ‘emp1’ is changed also to 1000. Explain why.
How will you disallow direct modification of the salary such as that given in line *** above?
Set the access to the constructor of Employee to ‘private’. Compile Employee.java and MyProg.java. Run MyProg. Explain what's happening.
Implementing a student class
Design a class to manage student results in a subject. A unique student number identifies each student. During the course of the subject, each student completes three assignments (representing 40% of the final mark but each scored out of 100) and an examination (also scored out of 100). The final mark is calculated by multiplying the sum of the assignments by 0.133 and the examination by 0.6 and adding the two products together. The class will allow a user to update an assignment mark or an examination mark, and print the final mark along with the student number for each student.
You must do the following:
Identify the class attributes and methods, draw the UML class diagram.
Write the java code for the class
مُحاولتي فِي الحَــل :
public class Student
{
int id;
double ass1, ass2, ass3, exam, finalMark;
public Student(int id, double ass1, double ass2, double ass3, double exam)
{
this.ass1 = ass1;
this.ass2 = ass2;
this.ass3 = ass3;
this.exam = exam;
this.id = id;
}
void finalResult(Student s) {
s.finalMark = 0.4 / 3 * (ass1 + ass2 + ass3) + 0.6 * exam;
System.out.println(s.finalMark);
}
public static void main(String[] args) {
Student s1 = new Student(0, 55, 66, 77, 90);
s1.finalResult(s1);
}
}*