Question

In: Computer Science

Core Java Do exercise 9a, 9b, 9c, & 9d with indented code and comments. 9a. public...

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

              }

}

Solutions

Expert Solution

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:


Related Solutions

Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
complete this code for me, this is java question. // Some comments omitted for brevity import...
complete this code for me, this is java question. // Some comments omitted for brevity import java.util.*; /* * A class to demonstrate an ArrayList of Player objects */ public class ListOfPlayers { // an ArrayList of Player objects private ArrayList players; public ListOfPlayers() { // creates an empty ArrayList of Players players = new ArrayList<>(); } public void add3Players() { // adds 3 Player objects players.add(new Player("David", 10)); players.add(new Player("Susan", 5)); players.add(new Player("Jack", 25)); }    public void displayPlayers()...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This algorithm is performed by moving the R/W head back-and-forth to the innermost and outermost track. As it scans the tracks from end to end, it process all the requests found in the direction it is headed. This will ensure that all track requests, whether in the outermost, middle or innermost location, will be traversed by the access arm thereby finding all the requests. This...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word: banana dresser grammar potato revive uneven assess Write a program that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters as lowercase letters.
I have to complete all //to do comments for the following code: /** * A ShoppingBasket...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket holds zero or more Products and can provide information * about the Products. One can add Products to a ShoppingBasket during its * lifetime, reset the ShoppingBasket, create a copy which contains Products of * at least a certain value, and make various queries to the ShoppingBasket. * (Thus, the number of Products that will be stored by a ShoppingBasket object * is not...
I needv pseudocode and a flowchart for the following java code public class AcmePay { public...
I needv pseudocode and a flowchart for the following java code public class AcmePay { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int hours, shift, retirement = 0; do { System.out.print("Enter the number of hours worked (>0): "); hours = scanner.nextInt(); } while (hours <= 0); do { System.out.print("Enter shift [1 2 or 3]: "); shift = scanner.nextInt(); } while (shift < 1 || shift > 3); if (shift == 2 || shift ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT