Question

/*--------------------------------------------------------------------------- // AUTHOR: // SPECIFICATION: This program is for practicing the use of classes, constructors, //...

/*---------------------------------------------------------------------------
// AUTHOR:
// SPECIFICATION: This program is for practicing the use of classes, constructors,
// helper methods, and the this operator.
// INSTRUCTIONS: Read the following code skeleton and add your own code
// according to the comments
//-------------------------------------------------------------------------*/

import java.util.Scanner;

public class
{
public static void main(String[] args)
{
// Let's make two students using all two constructors
// Write code to create a new student alice using constructor #1
//-->
Student alice =

// Write code to create a new student bob with name "Bob Brown",
// age 23 and GPA 3.8 using constructor #2
//-->


// Let's get user input to change the missing or default values
Scanner scan = new Scanner(System.in);

// Get user input and set Alice's full name
System.out.print("Enter full name for Alice: ");
alice.setName(scan.nextLine());

// Get user input and set Alice's age


// Write code to get user input and set Alice's GPA
//-->


// Lets print out the students details
alice
bob

System.out.println("\r\nChanging Bob's GPA");

// Get user input and change bob's gpa


// Write code to print out bob's new details
//-->


System.out.println("\r\nComparing Alice and Bob");

Student better = alice.betterGPA(bob);
System.out.println(better.getName() + " has a better GPA");

// Use the olderStudent method to figure out which student is
// older between Alice and Bob. Then print out the result. Follow
// the betterGPA example above.
//-->


// Finally, print out each student's details 3 times, using the print(int n) method
//-->

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source code

import java.util.Scanner;
class student
{
String Name;
int Age;
double GPA;
student(String name)
{
Name=name;
}
student(String name,int age, double gpa)
{
Name=name;
Age=age;
GPA=gpa;
}
void setName(String name)
{
Name=name;
}
void setAge(int age)
{
Age=age;
}
void setGPA(Double gpa)
{
GPA=gpa;
}
student betterGPA(student s)
{

if(s.GPA>this.GPA)
return s;
else
return this;
}
student olderStudent(student s)
{

if(s.Age>this.Age)
return s;
else
return this;
}
String getName()
{
return this.Name;
}
void print(int n)
{
for(int i=0;i<n;i++)
{
System.out.println("Name : "+this.Name +"\nAge : "+this.Age+"\nGPA ="+this.GPA+"\n");
}

}
public static void main(String[] args)
{
student alice =new student("Alice");
student bob = new student("Bob Brown",23,3.8);
Scanner scan=new Scanner(System.in);
System.out.println("Enter full name for Alice: ");
alice.setName(scan.nextLine());
System.out.println("Enter age for Alice: ");
alice.setAge(scan.nextInt());
System.out.println("Enter GPA for Alice: ");
alice.setGPA(scan.nextDouble());
System.out.println("*****Alice Details*****\nName : "+alice.Name +"\nAge : "+alice.Age+"\nGPA ="+alice.GPA);
System.out.println("\n*****Bob Details*****\nName : "+bob.Name +"\nAge : "+bob.Age+"\nGPA ="+bob.GPA);
System.out.println("\nChanging Bob's GPA :");
bob.GPA=scan.nextDouble();
System.out.println("\n*****Bob Details*****\nName : "+bob.Name +"\nAge : "+bob.Age+"\nGPA ="+bob.GPA);
System.out.println("\r\nComparing Alice and Bob");
student better = alice.betterGPA(bob);
System.out.println(better.getName() + " has a better GPA");
student older = alice.olderStudent(bob);
System.out.println(older.getName() + " is older student");
System.out.println("\n********Alice details*************");
alice.print(3);
System.out.println("\n********Bob details*************");
bob.print(3);
}
}

Sample output

*******************END***********PLS GIVE ME UPVOTE*************

Add a comment
Know the answer?
Add Answer to:
/*--------------------------------------------------------------------------- // AUTHOR: // SPECIFICATION: This program is for practicing the use of classes, constructors, //...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

  • Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables...

    Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables and 2 effectors for a new data type – the class HospitalPatient. You have the freedom to design the instance variables and effectors. instance variables effectors 1. 1. 2. 2. 3. Write the code for class HospitalPatient, according to the requirement above. Include the default constructors with no argument, and the constructor with all arguments. Provide a getter and setter for each individual instance...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • Write and submit the source code for the following program. The program will use an integer...

    Write and submit the source code for the following program. The program will use an integer array of size 10 to store the prices of smartphones. It will then determine and print the prices of the most expensive and cheapest phones. Use the following variables: int[] prices = new int[10]; // Array of smartphone prices Assignment Ask the user for the price of each smartphone (using a for loop) Sort the list of smartphones (once) from low to high price...

  • C++ Program classes READ THE FOLLOWING CAREFULLY TO GET FULL VALUE FROM THE PRACTICE. IF NOT...

    C++ Program classes READ THE FOLLOWING CAREFULLY TO GET FULL VALUE FROM THE PRACTICE. IF NOT COMFORTABLE WITH CLASSES, I WOULD START WITH DEFINING THE CLASS WITH ONE constructor AND GO FROM THERE Use the following to calculate a GPA (the following is for calculation information only): A = 4.00 grade points per credit hour A- = 3.70 grade points per credit hour B+ = 3.33 grade points per credit hour B = 3.00 grade points per credit hour B-...

  • C++ please Let's pretend that we can still go out to eat at a restaurant. Suppose...

    C++ please Let's pretend that we can still go out to eat at a restaurant. Suppose we have the job to make a program that accepts a dinner reservation. The information collected will be the name of the customer, the date for the reservation, the time for the reservation (good choice for a hierarchical struct), the number in the party, email address (for updates) and phone number. Define a hierarchical structure for a dinner reservation that will contain the above...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Java Program

    Write a program in Java to implement the min-priority queue using min-heap data structure. Implement the min-heap data structure using a String array of 5 cells. (Do not use Java in-built PriorityQueue class.) [In a min-heap, the root node and the intermediate node vales are always smaller than their children.] First, take 5 names from the user and insert them in the min-priority queue. Then print the elements of the queue. After that, delete two elements from the queue and...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT