In: Computer Science
Core Java
Do exercise 9a, 9b, 9c, & 9d with indented code and comments.
9a. public class Demo { public static void main(String[] args) { //create Employee object with int 101, String Sam,salary 1000 data //display this object data by passing to show method //add 100 bonus in salary //display this object data by passing to show method } public static void show(){ //do required changes in show method } } |
9b. public class Demo { public static void main(String[] args) { //create Employee object with int 101, String sam, salary 1000 data //display this object data by passing to show method //add 100 bonus in salary //display this object data by passing to show method } public void show(){ //do required changes in show method } } |
9c. In below example ,try to call show method in different ways which explains different ways to call static methods public class Demo { public static void main(String[] args) { //call show method in different ways } public static void show(){ System.out.println("show method"); } } |
9d. In below example, try to print total variable in different ways which explains different ways to call static variable public class Demo { private static int total = 10; public static void main(String[] args) { //print total in different ways } } |
Code For 9a:
public class Demo {
public static void main(String[] args) {
// create Employee object with int 101, String Sam,salary 1000 data
Employee e=new Employee(101,"Sam",1000);
// display this object data by passing to show method
show(e);
// add 100 bonus in salary
e.salary+=100;
// display this object data by passing to show method
show(e);
}
//Method to display employee details
public static void show(Employee e) {
System.out.println("\nEmployee Details: ");
System.out.println("Id: "+e.id);
System.out.println("Name: "+e.name);
System.out.println("Salary: "+e.salary);
}
}
//Employee Class required to create employee object
class Employee {
int id;
String name;
double salary;
public Employee(int id, String name, double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
}
Output Of Above Code:
Employee Details:
Id: 101
Name: Sam
Salary: 1000.0
Employee Details:
Id: 101
Name: Sam
Salary: 1100.0
Code For 9b:
public class Demo {
public static void main(String[] args) {
// create Employee object with int 101, String sam,salary 1000 data
Employee e=new Employee(101,"sam",1000);
// display this object data by passing to show method
show(e);
// add 100 bonus in salary
e.salary+=100;
// display this object data by passing to show method
show(e);
}
//Method to display employee details
public static void show(Employee e) {
System.out.println("\nEmployee Details: ");
System.out.println("Id: "+e.id);
System.out.println("Name: "+e.name);
System.out.println("Salary: "+e.salary);
}
}
//Employee Class required to create employee object
class Employee {
int id;
String name;
double salary;
public Employee(int id, String name, double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
}
Output Of Above Code:
Employee Details:
Id: 101
Name: sam
Salary: 1000.0
Employee Details:
Id: 101
Name: sam
Salary: 1100.0
Code For 9c:
public class Demo {
public static void main(String[] args) {
//Using Class Name
System.out.println("* * * Using Class Name * * * ");
Demo.show();
System.out.println("* * * Without any references * * * ");
show();
System.out.println("* * * Using Object reference * * * ");
Demo d=new Demo();
d.show();
}
public static void show() {
System.out.println("show method");
}
}
Output Of Above Code:
* * * Using Class Name * * *
show method
* * * Without any references * * *
show method
* * * Using Object reference * * *
show method
Code For 9d:
public class Demo {
private static int total = 10;
public static void main(String[] args) {
System.out.println("* * * Using Class name * * *");
System.out.println(Demo.total);
System.out.println("* * * Without Any reference * * *");
System.out.println(total);
System.out.println("* * * Using Object reference * * *");
Demo d=new Demo();
System.out.println(d.total);
}
}
Output Of Above Code:
* * * Using Class name * * *
10
* * * Without Any reference * * *
10
* * * Using Object reference * * *
10
Images Of Code:
Image Of Code 9a:
Image Of Code 9b:
Image Of Code 9c:
Image Of Code 9d:
Output Images:
Output Image Of 9a:
Output Image Of 9b:
Output Image Of 9c:
Output Image Of 9d: