public class Student{ //constant public static int SLOW = 1; public static int M
ID: 3659111 • Letter: P
Question
public class Student{ //constant public static int SLOW = 1; public static int MEDIOCRE = 2; public static int BRIGHT = 3; public static int studentCount = 0; private String name; private int age; private int smarts; //contructor.. has to have the same name as the class public Student(){ name = "John Doe"; age = 18; smarts = SLOW; ++studentCount; } //constructor.. this change the above private value public Student(String name, int age, int smarts){ this.name = newName; this.age = newAge; this.smarts = newSmarts; ++studentCount; } public static void showCount(){ System.out.println(studentCount); } public void setName(String newName){ name = newName; } public String sleep(int t1, int t2){ return ("Sleeps for " + (t2 - t1) + " hours"); } public int showSmart(){ return smarts; } } import java.util.Scanner; public class TestStudent{ public static void main(String[] arg){ Scanner in = new Scanner(System.in); Student.showCount(); Student s1 = new Student("John Doe", 18, Student.SLOW); //int x = in.nextInt(); s1.setName("Billy Bob"); System.out.println(Student.studentCount); Student s2 = new Student("Mary Jane", 20, Student.BRIGHT); System.out.println(Student.studentCount); System.out.println(s1.sleep(1, 3)); System.out.println(s2.sleep(1, 7)); } }Explanation / Answer
import java.util.Scanner; class Student { public static int SLOW = 1; public static int MEDIOCRE = 2; public static int BRIGHT = 3; public static int studentCount = 0; private String name; private int age; private int smarts; public Student() { name = "John Doe"; age = 18; smarts = SLOW; ++studentCount; } public Student(String newName, int newAge, int newSmarts) { this.name = newName; this.age = newAge; this.smarts = newSmarts; ++studentCount; } public static void showCount() { System.out.println(studentCount); } public void setName(String newName) { this.name = newName; } public String sleep(int t1, int t2) { return ("Sleeps for " + (t2 - t1) + " hours"); } public int showSmart() { return smarts; } } public class TestStudent { public static void main(String[] arg) { Scanner in = new Scanner(System.in); Student.showCount(); Student s1 = new Student("John Doe", 18, Student.SLOW); /* int x = in.nextInt(); */ s1.setName("Billy Bob"); System.out.println(Student.studentCount); Student s2 = new Student("Mary Jane", 20, Student.BRIGHT); System.out.println(Student.studentCount); System.out.println(s1.sleep(1, 3)); System.out.println(s2.sleep(1, 7)); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.