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...
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...
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'
Modify the original code and add an additional function of your choice. The function should be...
Modify the original code and add an additional function of your choice. The function should be unique and something you created for this assignment. Support your experimentation with screen captures of executing the new code. Prepare a test table with at least 3 distinct test cases listing input and expected output for your unique function. #include <stdio.h> void printHelp () { printf ("\n"); printf ("a: a(x) = x*x\n"); printf ("b: b(x) = x*x*x\n"); printf ("c: c(x) = x^2 + 2*x...
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 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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT