// You are provided with the Student class, which consists of two private variab
ID: 3565219 • Letter: #
Question
// You are provided with the Student class, which consists of two private variables- name, age. It also contains a constructor, and two methods i.e. toString // which will return the String representation of the Student object, and setAge which will set the Students age. //Import the Scanner class. //--> //Write the class header for the file Lab7 and the main method header. public class Lab7{ public static void main(String[] args){ //Instantiate an object of the Student class, called stInstance. Set the name and age as your own name and age. Use the constructor to do so. ??? ??? = new ???(???,???); // Print the Student details by calling the toString method. //--> // Instantiate an object of the Scanner class. //--> System.out.print("Please enter the student's new age: "); // Input the Students age from the user into a variable called newAge. //-> // Use the setAge method, to set the age of stInstance to the age you just read in from the user. ???.setAge(???); // Display the Student details again, and note how the age has been changed. //--> // Change the header for the Student class to private instead of public. Try to compile and run the program and note what happens. //--> ****** This is an experiment (dont include it in your submission) ******* // Print Fibonacci Series i.e 1, 1, 2, 3, 5, 8, 13, 21, ... // The formula is currVal= prevVal + prevPrevVal // Define 3 integer variables currVal, prevVal and prevPrevVal. Initialize prevVal and prevPrevVal variables to 1 // and initialize currVal to 2 //--> System.out.print("Fibonacci Series between 1 and 100: "); // Print out prevVal //--> // Print out prevPrevVal //--> // Write a do-while loop to print out Fibonacci numbers less than or equals 100 ???{ // Print out the <currVal> //--> // Assign <prevVal> to <prevPrevVal> //--> // Assign <currVal> to <prevVal> //--> // Calculate <currVal> using the formula given earlier //--> } // Check the while condition if <currVal> is less than or equals 100 ???(???) } }
Explanation / Answer
//Import the Scanner class.
import java.util.Scanner;
//Write the class header for the file Lab7 and the main method header.
public class Lab7{
public static void main(String[] args){
//Instantiate an object of the Student class, called stInstance. Set the name and age as your own name and age. Use the constructor to do so.
Student stInstance = new Student("Avinash",22); //IMPORTANT - change it to your own data
// Print the Student details by calling the toString method.
System.out.println(stInstance); //IMPORTANT - calling a print function for an instance of a class automatically calls the toString() method for that instance
// Instantiate an object of the Scanner class.
Scanner scanData = new Scanner(System.in);
System.out.print("Please enter the student's new age: ");
// Input the Students age from the user into a variable called newAge.
int newAge = scanData.nextInt();
// Use the setAge method, to set the age of stInstance to the age you just read in from the user.
stInstance.setAge(newAge);
// Display the Student details again, and note how the age has been changed.
System.out.println(stInstance);
// Print Fibonacci Series i.e 1, 1, 2, 3, 5, 8, 13, 21, ...
// The formula is currVal= prevVal + prevPrevVal
// Define 3 integer variables currVal, prevVal and prevPrevVal. Initialize prevVal and prevPrevVal variables to 1
// and initialize currVal to 2
int currVal , prevVal, prevPrevVal;
prevVal = prevPrevVal = 1;
currVal = 2;
System.out.print("Fibonacci Series between 1 and 100: ");
// Print out prevVal
System.out.print(prevVal + ", ");
// Print out prevPrevVal
System.out.print(prevPrevVal + ", ");
// Write a do-while loop to print out Fibonacci numbers less than or equals 100
do{
// Print out the <currVal>
System.out.print(currVal + ", ");
// Assign <prevVal> to <prevPrevVal>
prevPrevVal = prevVal;
// Assign <currVal> to <prevVal>
prevVal = currVal;
// Calculate <currVal> using the formula given earlier
currVal = prevVal + prevPrevVal;
} // Check the while condition if <currVal> is less than or equals 100
while(currVal<=100);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.