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

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...
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'
QUESTION: Add code so that if no parameter values are specified for method “main”, then the...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line … System.exit(0); skeleton code: /**...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
---In the code, create add and delete a student by ID number when prompted /////////////////////////////////////////////////////////////// import...
---In the code, create add and delete a student by ID number when prompted /////////////////////////////////////////////////////////////// import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String [] students = new String[1];     private int numberOfStudents;     public COurseCom66(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getCourseName() {         return courseName;     }     public...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
in digestive system the main and accessory organs and their main function.? type don't write
in digestive system the main and accessory organs and their main function.? type don't write
2. Specification - Given the following code base, add the appropriate function that will give the...
2. Specification - Given the following code base, add the appropriate function that will give the correct results/output. CODE: # TODO : Add Two Missing Functions HERE mlist = [(" Orange ", 10 , 0.25) ,( " Apple ", 5 , .20) , (" Banana ", 2 , 0.3) ,(" Kiwi ", 1 , 0.5)] addFruit (10 ," Lemon " ,0.1) displayFruitList ( mlist ) OUTPUT: Orange --- $ 2.50 Apple --- $ 1.00 Banana --- $ 0.60 Kiwi ---...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT