Having a few more issues with my homework in debugging it all and making it work
ID: 3607445 • Letter: H
Question
Having a few more issues with my homework in debugging it all and making it work entirely.
PERSONTESTER.JAVA___
import java.util.*;
import java.io.*;
public class PersonTester extends Person
{
public static void main(String[] args) throws IOException
{
String text = null;
String a = "data1.txt";
Scanner read=new Scanner(new File(a));
// BufferedReader brst = new BufferedReader(new FileReader(a));
//text = brst.readLine();
int aSize = Integer.parseInt(read.nextLine());
System.out.println(aSize);
Student[] students=new Student[aSize];
int i=0;
while(read.hasNext())
{
Student s=new Student();
s.name=read.next();
s.age=read.nextInt();
s.id = read.nextInt();
s.major = read.next();
students[i]=s;
i++;
}
sortStudents(students);
System.out.println(Arrays.toString(students));
}
//Scanner in=new Scanner(new File(b));
// int bSize = Integer.parseInt(in.nextLine());
//System.out.println(bSize);
//same for DATA2.txt just like data1.txt
//same for data2.txt
public static void sortStudents(Student students[])
{
int size = students.length;
for (int p=1; p {
Student key = students[p];
int q = p-1;
while (q>=0 && students[q].compareTo(key)==1)
{
students[q+1] = students[q];
q = q-1;
}
students[q+1] = key;
}
}
}
STUDENT.JAVA____
import java.io.*;
import java.util.*;
public class Student extends Person
{
private String major;
private int sId;
public Student(String n, int age, int d, String m)
{
super(n, age);
major = m;
sId = d;
}
public int getID(){
return sId;
}
public String getMajor(){
return major;
}
public void setID(){
this.sId = sId;
}
public void setMajor(){
this.major = major;
}
public String toString()
{
return "Student[super=" + super.toString() + ",major=" + major + "]";
}
}
INSTRUCTOR______
import java.util.*;
import java.io.*;
public class Instructor extends Person
{
private double salary;
private int result;
public Instructor(String n, int age, double s)
{
super(n, age);
salary = s;
}
public double getSalary(){
return salary;
}
public void setSalary(){
this.salary = salary;
}
public String toString() {
{
return "Employee[" + super.toString() + ",salary=" + salary + "]";
}
}
}
PERSON___(THIS CLASS CAN NOT CHANGE)
public class Person
{
private String name;
private int age;
public Person()
{
name="";
age=0;
}
/**
Create a person with a given name and age.
@param name the name
@param age the age
*/
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
/**
Get the name.
@return the name
*/
public String getName()
{
return name;
}
/**
Change the name.
@param name the name
*/
public void setName(String name)
{
this.name = name;
}
/**
Get the age.
@return the age
*/
public int getAge()
{
return age;
}
/**
Change the age.
@param age the age
*/
public void setAge()
{
this.age = age;
}
/**
Convert person to string form.
*/
public String toString()
{
return "Name: " + name + " " + "Age: " + age;
}
}
Explanation / Answer
Hello, I have modified your code exactly as per your requirements.
//Person.java
public class Person {
private String name;
private int age;
public Person() {
name = "";
age = 0;
}
/**
* Create a person with a given name and age.
*
* @param name
* the name
* @param age
* the age
*/
public Person(String name, int age) {
this.name = name;
this.age = age;
}
/**
* Get the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Change the name.
*
* @param name
* the name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the age.
*
* @return the age
*/
public int getAge() {
return age;
}
/**
* Change the age.
*
* @param age
* the age
*/
public void setAge() {
this.age = age;
}
/**
* Convert person to string form.
*/
public String toString() {
return "Name: " + name + " " + "Age: " + age;
}
}
//Student.java
public class Student extends Person implements Comparable<Student>{
private String major;
private int sId;
public Student(String n, int age, int d, String m) {
super(n, age);
major = m;
sId = d;
}
public int getID() {
return sId;
}
public String getMajor() {
return major;
}
public void setID() {
this.sId = sId;
}
public void setMajor() {
this.major = major;
}
/**
* this method will return a string containing all the details in proper format
*/
@Override
public String toString() {
String str=super.toString(); /*getting name and age by calling superclass toString()*/
str+=" ID: "+sId+" Major: "+major; /* appending ID and Major to the string*/
return str;
}
/**
* This method will compare two student objects and helps to sort the students based
* on student ID in ascending order
*/
public int compareTo(Student s) {
if(this.getID()>s.getID()){
return 1;
}else{
return -1;
}
}
}
//Instructor.java
import java.util.*;
import java.io.*;
public class Instructor extends Person implements Comparable<Instructor> {
private double salary;
private int result;
public Instructor(String n, int age, double s) {
super(n, age);
salary = s;
}
public double getSalary() {
return salary;
}
public void setSalary() {
this.salary = salary;
}
/**
* this method will return a string containing all the details in proper format
*/
@Override
public String toString() {
String str=super.toString();
String sal=String.format("%.2f", salary); /*rounding salary into 2 decimal places*/
sal=String.format("%10s", sal); /*right aligning*/
str+=" Salary: "+sal;
return str;
}
/**
* This method will compare two Instructor objects and helps to sort the instructors based
* on salary in ascending order
*/
public int compareTo(Instructor i) {
if(this.getSalary()>i.getSalary()){
return 1;
}else{
return -1;
}
}
}
//PersonTester.java
import java.util.*;
import java.io.*;
public class PersonTester extends Person {
public static void main(String[] args) throws IOException {
String text = null;
String stud = "data1.txt"; /*student data file name*/
String ins="data2.txt";/*instructor data file name*/
Scanner read = new Scanner(new File(stud)); /*scanner object to read student data file*/
int aSize = Integer.parseInt(read.nextLine());
Student[] students = new Student[aSize];
int i = 0;
while (read.hasNextLine()) {
String name = read.next().trim();
int age = read.nextInt();
int id = read.nextInt();
String major = read.next().trim();
Student s = new Student(name, age, id, major); /*creating an object by passing parameters*/
students[i] = s;/*adding to the array*/
i++;
}
read = new Scanner(new File(ins)); /*to read instructor data file*/
int iSize=Integer.parseInt(read.nextLine());
Instructor[] instructors=new Instructor[iSize];
i=0;
while (read.hasNextLine()) {
String name = read.next().trim();
int age = read.nextInt();
double salary = read.nextDouble();
Instructor instructor=new Instructor(name, age, salary);/*creating an object by passing parameters*/
instructors[i] = instructor; /*adding to the array*/
i++;
}
/**
* Sorting the student array, this will invoke the compareTo() method to sort each objects
*/
Arrays.sort(students);
System.out.println("A List of Students sorted by StudentID:");
for(int j=0;j<students.length;j++){
System.out.println(students[j].toString());
}
/**
* Sorting the instructor array
*/
Arrays.sort(instructors);
System.out.println(" A List of Instructors sorted by salary:");
for(int j=0;j<instructors.length;j++){
System.out.println(instructors[j].toString());
}
}
}
//data1.txt
8
Larry 20 12345 CPSC
Amy 23 34343 IT
Ashley 18 99923 ENGL
John 17 23434 CPSE
Eric 19 11111 Engineering
Jennifer 20 33333 Biology
Christina 19 55555 French
Mike 21 77777 Chemistry
//data2.txt
6
Steve 50 98000.50
Jenny 29 35343.00
Chris 35 46233.000
Alex 42 55599.21
Shala 55 150010.23
Kathy 45 39888.43
/*Output*/
A List of Students sorted by StudentID:
Name: Eric Age: 19 ID: 11111 Major: Engineering
Name: Larry Age: 20 ID: 12345 Major: CPSC
Name: John Age: 17 ID: 23434 Major: CPSE
Name: Jennifer Age: 20 ID: 33333 Major: Biology
Name: Amy Age: 23 ID: 34343 Major: IT
Name: Christina Age: 19 ID: 55555 Major: French
Name: Mike Age: 21 ID: 77777 Major: Chemistry
Name: Ashley Age: 18 ID: 99923 Major: ENGL
A List of Instructors sorted by salary:
Name: Jenny Age: 29 Salary: 35343.00
Name: Kathy Age: 45 Salary: 39888.43
Name: Chris Age: 35 Salary: 46233.00
Name: Alex Age: 42 Salary: 55599.21
Name: Steve Age: 50 Salary: 98000.50
Name: Shala Age: 55 Salary: 150010.23
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.