Java Assignment using netbeans Requirements One class, one file . Don\'t create
ID: 3905158 • Letter: J
Question
Java Assignment using netbeans
Requirements
One class, one file. Don't create multiple classes in the same .java file.
Don't use static variables and methods
Encapsulation: make sure you protect your class variables and provide access to them through get and set methods
Create 6 classes:
app
address
zipcode
student
group
person
The application app will:
create 4 addresses
create 4 students
create a group with a name and 4 students
Then using the group object that was createddisplay the group information using the group method displayInfo( )
displayInfo( ) uses the method getInfo( ) in the student class
the students will be listed sorted by last name as specified below.
The address class has the following attributes
street (a string with address number and street name)
city
state
zipcode
The zipcode class has the following attributes:
number
complement
The person class has the following
attributes:
first name
last name
age
address
methods
getInfo( ) which returns the complet person information as a String
The student class extends the class person and implements the Comparable interface, and has the following
attributes:
id
major
gpa
methodsgetInfo( ) which returns the complet student information as a String
getInfo( ) overrides getInfo( ) from person
compareTo( )
compareTo( ) overrides compareTo( ) from Comparable
The group class has the following attributes:
name
4 students in an array
the array should be sorted by last name
sort will be done using the sort method available in Java's Arrays (Links to an external site.)Links to an external site. class or in the ArrayList (Links to an external site.)Links to an external site.class, that's why you need to implement the comparable interface in the student class.
methodsgetInfo() which returns a String with the group's name and student's information
getInfo uses the student's method getInfo( ) to retrive each student's information
displayInfo( ) uses a "System.out.println" to display the group name and students information
getAverageGPA() which calculates the average GPA in the group and returns a double number
search(int n) which searches for a student in the group with the id as input parameter and returns a student object. If the student is not found, it returns null
Contents person First name Last name Age Address getlnfo) Comparable compareTo() address Street City State Zipcode zipcode number Complement student Id Major GPA getlnfo) app group Creates 4 addresses Creates 4 students Creates a group with 4 students Name Sorted Array with 4 Students 1-Displays name of the group and complete information about al students in the group using the displaylnfo() method in group 2- displaylnfo) relies on student's getlnfo() to display information of each student getlnfo() displaylnfol()Explanation / Answer
Complete code:
person class:
public class person
{
//attributes
private String firstName,lastName;
private address a;
private int age;
//default constructor
public person() {}
//parameterized constructor
public person(String firstName, String lastName, address a,int age)
{
super();
this.firstName = firstName;
this.lastName = lastName;
this.a = a;
this.age = age;
}
//getter and setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public address getAddress() {
return a;
}
public void setAddress(address a) {
this.a = a;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//method getinfo()
public String getInfo()
{
return "First Name:"+firstName+",Last Name:"+lastName+",Address:"+a.toString()+",Age:"+age;
}
}
student class:
public class student extends person implements Comparable<student>
{
//attributes
private int id;
private String major;
private double gpa;
//default constructor
public student() {}
//parameterized constructor
public student(String firstName, String lastName, address ad, int age,
int id, String major, double gpa)
{
super(firstName,lastName,ad,age);
this.id = id;
this.major = major;
this.gpa = gpa;
}
//getter and setter methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
//method getinfo()
public String getInfo()
{
String person=super.getInfo();
return person+"id:"+id+",major:"+major+",gpa:"+gpa;
}
//compareTo( ) overrides compareTo( ) from Comparable
public int compareTo(student obj)
{
return super.getLastName().compareTo(obj.getLastName());
}
}
address class:
public class address {
//attributes
private String street,city,state;
private zipcode zc;
//default constructor
public address() {}
//parameterized constructor
public address(String street, String city, String state, zipcode zc)
{
super();
this.street = street;
this.city = city;
this.state = state;
this.zc = zc;
}
//getter and setter methods
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public zipcode getZipcode() {
return zc;
}
public void setZipcode(zipcode zc) {
this.zc = zc;
}
@Override
public String toString()
{
return "address [street=" + street + ", city=" + city + ", state=" + state + ", zipcode=" + zc + "]";
}
}
zipcode class:
public class zipcode
{
//attributes
private int number;
private int complement;
public zipcode() {}
//parameterized constructor
public zipcode(int number, int complement)
{
super();
this.number = number;
this.complement = complement;
}
//getter and setter methods
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getComplement() {
return complement;
}
public void setComplement(int complement) {
this.complement = complement;
}
@Override
public String toString()
{
return "zipcode [number=" + number + ", complement=" + complement + "]";
}
}
group class:
public class group {
//attributes
private String name;
private student[] ar;
public group() {}
public group(String name, student[] ar)
{
this.name = name;
this.ar = new student[4];
}
//getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public student[] getAr() {
return ar;
}
public void setAr(student[] ar) {
this.ar = ar;
}
public String getInfo()
{
String output="Group Name:"+name;
for(int i=0;i<ar.length;i++)
{
output=output+" "+ar[i].getInfo();
}
return output;
}
public void displayInfo()
{
String output="Group Name:"+name;
for(int i=0;i<ar.length;i++)
{
output=output+" "+ar[i].getInfo();
}
System.out.println(output);
}
public double getAverageGPA()
{
double GPA=0;
for(int i=0;i<ar.length;i++)
{
GPA=GPA+ar[i].getGpa();
}
return GPA;
}
public student search(int n)
{
student s=null;
for(int i=0;i<ar.length;i++)
{
if(ar[i].getId()==n)
{
s=ar[i];
break;
};
}
return s;
}
}
app class:
import java.util.Arrays;
public class app
{
public static void main(String[] args)
{
//creating 4 address
address add1= new address("4th Street","London","London",new zipcode(1,10));
address add2= new address("12thCroass","UK","UK",new zipcode(2,20));
address add3= new address("Steven George streen","Mexico","Mexico",new zipcode(3,30));
address add4= new address("Paul's Road","Newyork","Newyork",new zipcode(4,40));
//creating 4 students
student s1=new student("Joyebo","Lukas",add1,26,1210,"CSE",3.7);
student s2=new student("Mathew","Bryni",add2,23,1210,"ECE",3.3);
student s3=new student("Karenl","Pawel",add3,24,1430,"CSE",3.4);
student s4=new student("Parins","Rahul",add4,28,2342,"ECE",3.8);
//creating a group with 4 students
group grp=new group();
student[] ar= new student[4];
ar[0]=s1;
ar[1]=s2;
ar[2]=s3;
ar[3]=s4;
//to sort student's last name
Arrays.sort(ar);
//set group name and
grp.setName("Group-1");
//set a group with 4 students
grp.setAr(ar);
//calling displayInfo() method of group
grp.displayInfo();
System.out.println(grp.getInfo());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.