Question

In Java Let’s say we’re designing a Student class that has two data fields. One data...

In Java Let’s say we’re designing a Student class that has two data fields. One data field, called id, is for storing each student’s ID number. Another data field, called numStudents, keeps track of how many Student objects have been created. For each of the blanks, indicate the appropriate choice.
id should be   public/private   and   static/not static .
numStudents should be   public/private   and   static/not static .


The next three questions use the following class:
class Counter {
    private int count;
  
    public Counter() {
        this(0);
    }
  
    public Counter(int startingCount) {
        count = startingCount;
    }
  
    public void increment() {
        count++;
    }
  
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}


Here is one program using the above class:
public class Main {
   public static void main(String[] args) {
       Counter c1 = new Counter();
       Counter c2 = new Counter(5);
       swap(c1.getCount(), c2.getCount());
       System.out.print(c1.getCount() + " " +
                           c2.getCount());
   }
  
   public static void swap(int c1, int c2) {
        int temp = c1;
        c1 = c2;
        c2 = temp;
   }
}
If you run the program, you’ll see that the output is:
0 5
Explain using words and/or pictures why that is the output.


Here is a second program using the above class:
public class Main {
   public static void main(String[] args) {
       Counter c1 = new Counter();
       Counter c2 = new Counter(5);
       swap(c1, c2);
       System.out.print(c1.getCount() + " " +
                           c2.getCount());
   }
  
   public static void swap(Counter c1, Counter c2) {
        Counter temp = c1;
        c1 = c2;
        c2 = temp;
   }
}
If you run the program, you’ll see that the output is:
0 5
Explain using words and/or pictures why that is the output.


Here is a third program using the above class:
public class Main {
   public static void main(String[] args) {
       Counter c1 = new Counter();
       Counter c2 = new Counter(5);
       swap(c1, c2);
       System.out.print(c1.getCount() + " " +
                           c2.getCount());
   }
  
   public static void swap(Counter c1, Counter c2) {
        int temp = c1.getCount();
        c1.setCount(c2.getCount());
        c2.setCount(temp);
   }
}
If you run the program, you’ll see that the output is:
5 0
Explain using words and/or pictures why that is the output.

Homework Answers

Answer #1

Answer 1:
id should be private and non static:
numStudents should be public and static

Answer 2:
because c1 and c2 has 0 and 5 we are calling swap function
but here the we are passing parameters using call by value
so that swap occured in that function will not have impact at main

Answer 3:
because c1 and c2 has 0 and 5 we are calling swap function
but here the we are passing parameters using call by value
so that swap occured in that function will not have impact at main

Answer 4:here we are changing the values of c1 and c2 in swap function so the chages will
have effect at main

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
Let’s say we’re designing an Employee class that has two data fields. One data field, called...
Let’s say we’re designing an Employee class that has two data fields. One data field, called id, is for storing each employee’s ID number. Another data field, called numEmployees, keeps track of how many Employee objects have been created. For each of the blanks, indicate the appropriate choice. id should be ( public/private ) and ( static/not static ) numEmployees should be (public/private) and (static/not static )
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
What is the output of the following Java program? class Food { Food() { System.out.println(flavor); }...
What is the output of the following Java program? class Food { Food() { System.out.println(flavor); } String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; } public class Lunch { public static void main(String[] args) { Food lunch = new Pepper(); } } Select one: a. spicy b. the program does not compile c. bland spicy d. no output e. bland
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...