Question

JAVA Problem: Student is a class that has the following instance variables: name (a String), yearInSchool...

JAVA Problem:

Student is a class that has the following instance variables: name (a String), yearInSchool (an int), and gpa (a double). Write a constructor for the Student class that takes parameters for these instance variables as initial values of the variables. Use the this reference in the constructor.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.*;

class Student
{
String name;
int yearInSchool;
double gpa;

Student(String name,int yearInSchool,double gpa)
{
this.name=name;
this.yearInSchool=yearInSchool;
this.gpa=gpa;
}

public String toString()
{
String output="";
output=output+"Name: "+name+"\n";
output=output+"Year in School: "+yearInSchool+"\n";
output=output+"GPA: "+gpa+"\n";
return output;
}
}

class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter Name: ");
String nm=sc.nextLine();
System.out.print("Enter year in school: ");
int year=sc.nextInt();
System.out.print("Enter GPA: ");
double gpa1=sc.nextDouble();
System.out.println("\n");

Student student=new Student(nm,year,gpa1);
System.out.println("Student Details: ");
System.out.println("------------------------------\n");
System.out.println(student+"\n");
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
JAVA Problem: Student is a class that has the following instance variables: name (a String), yearInSchool...
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
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