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!
Please do in java with code available for copy and with comments so I can follow...
Please do in java with code available for copy and with comments so I can follow along :)\ Develop a program that prints out the sum of each column of a two-dimensional array. The program defines method sumColumn() takes a two-dimensional array of integers and returns a single-dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and then calls method sumColumns()....
Add comments to the following code: PeopleQueue.java import java.util.*; public class PeopleQueue {     public static...
Add comments to the following code: PeopleQueue.java import java.util.*; public class PeopleQueue {     public static void main(String[] args) {         PriorityQueue<Person> peopleQueue = new PriorityQueue<>();         Scanner s = new Scanner(System.in);         String firstNameIn;         String lastNameIn;         int ageIn = 0;         int count = 1;         boolean done = false;         System.out.println("Enter the first name, last name and age of 5 people.");         while(peopleQueue.size() < 5) {             System.out.println("Enter a person");             System.out.print("First Name: ");             firstNameIn...
using java with proper header and comments: This exercise will give you a review of Strings...
using java with proper header and comments: This exercise will give you a review of Strings and String processing. Create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT