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
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.
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);...
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...
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);...
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
What is the output of the following Java program? class Food { String flavor = "bland";...
What is the output of the following Java program? class Food { String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; Pepper(String flavor) { this.flavor = flavor; } } public class Lunch { public static void main(String[] args) { Pepper pepper = new Pepper("sweet"); System.out.println(pepper.flavor); } } Select one: a. bland b. the program does not compile c. no output d. spicy e. sweet
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
//What is the output? import java.util.HashMap; public class AirportCodes {    public static void main (String[]...
//What is the output? import java.util.HashMap; public class AirportCodes {    public static void main (String[] args) {       HashMap<String, String> airportCode = new HashMap<String, String>();       airportCode.put("GRX", "Granada, Spain");       airportCode.put("ALB", "Albany, USA");       airportCode.put("IWJ", "Iwami, Japan");       System.out.print("ALB: ");       System.out.println(airportCode.get("ALB"));       airportCode.put("IWJ", "Ivalo, Finland");       System.out.print("IWJ: ");       System.out.println(airportCode.get("IWJ"));    } }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT