2C.Let S Write Another Class and Test It with a Driver. This Time Let S Create a Student

2C.Let S Write Another Class and Test It with a Driver. This Time Let S Create a Student

2C.Let’s write another class and test it with a driver. This time let’s create a Student class that models a Loyola student. The Student class should have instance variables for the student’s first and last name, and at least TWO other instance variables chosen by you. Include a zero-argument and multi-argument constructor to initialize all instance variables. Choose the most appropriate variable types (i.e. int, double, String, boolean) for each instance variable. It should also have a toString() method to display the object’s instance variables in a user-friendly format.

public class Student

private String firstName;

private String lastName;

privateintgradeLevel;

privateint age;

public Student()

firstName = "John";

public Student( String firstName, String lastName, intgradeLevel, int age )

this.firstName = firstName;

public String toString()

{

String studentInfo = new String();

studentInfo = ( "First Name:" + firstName + "\n" +

Write a separate “StudentDriver” class to:

importjavax.swing.JOptionPane;

public class StudentDriver

{

public static void main( String args[] )

a) create an instance of a Student called “senior001” using its zero-argument constructor, Student senior001 = new Student();

b) display the instance variables of the “senior001” using the object’stoString()method, JOptionPane.showMessageDialog( null, senior001, "Senior 1", JOptionPane.INFORMATION_MESSAGE );

c) create an instance of a Student called a “frosh001” and initialize its instance variables using the object’s multi-argument constructor,

Student frosh001 = new Student( "Joseph", "Castelan", 9, 14 );

d)display the instance variables of the “frosh001” using the object’s toString()method.

JOptionPane.showMessageDialog( null, frosh001, "Frosh 1", JOptionPane.INFORMATION_MESSAGE );

If you are looking for an extra challenge, try to printout the results of the program using JOptionPane.showMessageDialog()

P2D.Design a class that models an object of your choice (e.g. type of car, sports team, musical instrument, etc.) Include at least two different instance variables of two different types (e.g. String and an int, int and a double, boolean and a String, etc.) for your class. It should also have a toString() method to display the object’ instance variables in a user-friendly format.

public class Car

private double pri

private String co

privateint num

public Car()

price = 0;

color = " ";

numberOfSeats = 0;

public Car( double price, String color, intnumberOfSeats )

this.price = price;

public String toString()

{

String carInfo = new String();

carInfo = "Price: " + price + "\n" +

Write a separate “YourObjectDriver” class to:

public class CarDriver

public static void main( String[] args )

Car car1 = new Car();

Car car2 = new Car( 500000.75, "Red", 5);

a)create an instance of a your object called “obj1” (or a name more appropriate for your object) using its zero-argument constructor,

b)display the instance variables of the “obj1” using the object’s toString() method,

c)create an instance of a your object called a “obj2” and initialize its instance variables using the object’s multi-argument constructor, and

d)display the instance variables of the “obj2” using the object’s toString() method.