You can use this assignment in your class!
Mastery Progress(100.0%)
i

Unit 5: Writing Classes - Review Assignment

Use a static variable to count instances of a class

Create a class named Student with a static variable count of type int. Initialize it to 0. Then, create a constructor for the class that increases the value of count by 1 every time a new instance of the class is created. Finally, create a static method named getCount that returns the current value of count.

You answeredJUST NOW
Java
import java.util.Scanner;
class Student {
// Declare your static variable here
// Implement your constructor here
// Implement your method here
}
// Testing mechanism - do not modify!
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for (int i=0; i<num; i++) {
new Student();
}
System.out.println(Student.getCount());
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You passed all test cases!
STDIN
10
Expected STDOUT
10
Your STDOUT
10
STDIN
35
Expected STDOUT
35
Your STDOUT
35
STDIN
103
Expected STDOUT
103
Your STDOUT
103

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Alec KretchAPR 22, 2024, 7:17:55 PM
Java
import java.util.Scanner;
class Student {
static int count = 0;
Student() {
count++;
}
static int getCount() {
return count;
}
}
// Testing mechanism - do not modify!
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for (int i=0; i<num; i++) {
new Student();
}
System.out.println(Student.getCount());
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Was this helpful?

(Voting helps us personalize your learning experience!)
Think you've got it?

What will be the value of count after creating 5 instances of the Student class?

Select one of the following options:
  • A.

    1

  • B.

    0

  • C.

    10

  • D.

    5

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

You may exit out of this review and return later without penalty.