Question

In: Computer Science

Add two lines of code in main() function to create a variable, goodStudent, whose type is...

Add two lines of code in main() function to create a variable, goodStudent, whose type is Student with name, "John" and age, 21.

public class Student {
   private String name;
   private int age;

   public Student(){
      name = "";
      age= 0;
   }

   public Student(String initName){
      name = initName;
      age = 0;
   }

   public String getName() {
      return name;
   }

   public void setAge(int anAge) {
       age = anAge;
   }

   public static void main (String[] args) {

        // !!!! Your two lines of code come here   !!!
    
   }
}

Solutions

Expert Solution

Program:

Explanation:

Line 26: Here creating object called goodStudent for class Student by passing string parameter  

Line 27: calling setAge() method using object goodStudent

Program:

public class Student {
private String name;
private int age;

public Student(){
name = "";
age= 0;
}

public Student(String initName){
name = initName;
age = 0;
System.out.println(name);
}

public String getName() {
return name;
}

public void setAge(int anAge) {
age = anAge;
System.out.println(age);
}

public static void main (String[] args) {
Student goodStudent=new Student("John");
goodStudent.setAge(21);

}
}

Output:


Related Solutions

Using Python, create a function whose inputs are two matrices and whose output is the element...
Using Python, create a function whose inputs are two matrices and whose output is the element wise product of the two matrices.
Create all necessary code to make this main function work. It is not allowed to change...
Create all necessary code to make this main function work. It is not allowed to change the main function. int main() {        int ListDataSample1[] = { 1, 1, 1 };        int ListDataSample2[] = { 2, 2, 2 };        List<int> List1 = List<int>(ListDataSample2, 3);        List<int> List2 = List<int>(ListDataSample2, 3);               cout << "List1 :" << List1 << endl;        cout << "List2 :" << List2 << endl << endl;        List1 += List2;               cout...
Describe the boundary lines for two- variable inequalities. Why are the boundary lines for two- variable...
Describe the boundary lines for two- variable inequalities. Why are the boundary lines for two- variable inequalities with greater than and less represented by dotted lines? Provide examples. First, define a boundary line and tell where it comes from. Then, describe what the boundary line can tell us about solutions to an inequality. You can also talk about how to know what part of a graph to shade. Finally , talk about the cases where we use each type of...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed … what you did to fix it. import java.util.Scanner; public static void main(String[] args) { // This program Converts grade Points into a Letter grade. int gradePoints == 00; // Declare variable and assign initial value System.out.printl("Enter Grade Points: "; //Input gradePoints; if ( gradePoints >= -42 ) { System.out.println("Grade = A"); } // if true, then ends here, if false drop to next...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to read integers from the keyboard with the method nextInt. Use a try catch block to capture non-integer input, and tell the user, "This is not an Integer". The program loop should end when the user enters the string "quit". (Hint: "quit" is clearly not a number and will be captured in the try / catch block.) A typical Output Dialog is shown below: Enter...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: coinflip.js For this program you will have two functions, one called main and the second called flip. This program is also required the use of a loop construct. Write...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: cookout.js Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8. Write a program called cookout.js, that calculates the number of...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var triangle.js Write a program that is required to use nested loops to generate a triangle as shown in the sample run below. The program should begin by prompting the user...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT