Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Design and write a Java program which defines a class of your choicek. Provide over-loaded constructors and use them in the main method. This

JAVA

Design and write a Java program which defines a class of your choicek. Provide over-loaded constructors and use them in the main method. This is an open-ended assignment; Grading is based on "providing overloaded constructors" for your class and using them to create objects in main method. Atleast three overloaded constructors must be present in your class. Some suggestions for your class could be based on Rectangle Circle Person Student Faculty Course etc.

//

import java.util.Scanner;

/** * You do not have to use an array */ public class AssignmentTwo { public static void main(String[] args) {

// number of students int n = Integer.parseInt(args[0]); Scanner studentScanner = new Scanner(System.in);

// initialize an arary that holds n objects of type Student Student[] students = new Student[n];

Student graduate = new Student("Jean Luc", "Picard"); // read in the data // This section needs some refinement since I/O can be tricky via cmd line/interaction for (int i = 0; i < n; i++) { // Use Scanner to read in Student details System.out.println("Enter first name"); String first = studentScanner.nextLine(); System.out.println("Enter last name"); String last = studentScanner.nextLine(); System.out.println("Enter email address"); String email = studentScanner.nextLine(); System.out.println("Enter section number"); int section = studentScanner.nextInt(); /* Use the constructor */ students[i] = new Student(first, last, email, section); } // insertion sort students in ascending order of section for (int i = 0; i < n; i++) { for (int j = i; j > 0; j--) { if (students[j].less(students[j-1])) { Student swap = students[j]; students[j] = students[j-1]; students[j-1] = swap; } } } // print results for (int i = 0; i < n; i++) { System.out.println("Student Data" + students[i]); } System.out.println("Graduate Student details " + graduate.getFirst()+ " " + graduate.getLast()); } }

class Student { private final String first; // first name private final String last; // last name private String email; // email address private int section; // section number

// construct a new student with given fields public Student(String first, String last, String email, int section) { this.first = first; this.last = last; this.email = email; this.section = section; } public String getFirst() { return first; } public String getLast() { return last; }

public Student(String first, String last) { this.first = first; this.last = last; } // return true if the invoking object's section is less than that of b public boolean less(Student b) { Student a = this; return a.section < b.section; } // return a string representation of the invoking object public String toString() { return section + " " + first + " " + last + " " + email; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions