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.
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
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...
What's wrong with this code? #Java Why I am getting error in : int BusCompare =...
What's wrong with this code? #Java Why I am getting error in : int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); public class TypeComparator implements Comparator<Type> { @Override public int compare(Type o1, Type o2) { // return o1.name.compareTo(o2.name); int NameCompare = o1.name.compareTo(o2.name); int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); // 2-level comparison using if-else block if (NameCompare == 0) { return ((BusCompare == 0) ? NameCompare : BusCompare); } else { return NameCompare; } } } public class Type implements Comparable<Type> { int id; String name; 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);...
public class MyClass {   public static void main(String[] args)   {     for (int x = 1; x...
public class MyClass {   public static void main(String[] args)   {     for (int x = 1; x <= 10; x++)     {       System.out.print("-x" + x);            }   } } If this simple program is compiled and run, the following is written to the standard output.
-x1-x2-x3-x4-x5-x6-x7-x8-x9-x10 Add only one statement to the line 8. As a result of that the program writes “-x1-x7” to the standard output.
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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT