In: Computer Science
Java Programmig Enumeration Assignment.
What Will You Learn
Using enumerations to ensure data integrity and using enumerations for making business decisions.
Deliverables
Contents
Using inheritance and the classes (below)
Create an application (main or app) that:
Solution:
Givendata:
Please create a package name app and create following class in this package
AddressType is enum
Compile All classes and run only App class
/*******************************Person.java*******************************/
package app;
import java.util.ArrayList;
import java.util.List;
/**
* @author
*
* class Person
*/
public class Person {
private String firstName;
private String lastName;
private int age;
private String homeTown;
private List<Address> address;
/**
* Default constructor to initialize instance variable
to initial value
*/
public Person() {
this.firstName = "";
this.lastName = "";
this.age = 0;
this.homeTown = "";
this.address = new
ArrayList<Address>();
}
/**
* Parameterized constructor to initialize instance
variables to given
* parameters
*
* @param firstName
* @param lastName
* @param age
* @param homeTown
* @param address
*/
public Person(String firstName, String lastName, int
age, String homeTown, List<Address> address) {
this.firstName =
firstName;
this.lastName = lastName;
this.age = age;
this.homeTown = homeTown;
this.address = address;
}
// return first name
public String getFirstName() {
return firstName;
}
// set first name
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// return last name
public String getLastName() {
return lastName;
}
// set last name
public void setLastName(String lastName) {
this.lastName = lastName;
}
// return age
public int getAge() {
return age;
}
// set age
public void setAge(int age) {
this.age = age;
}
// return home town
public String getHomeTown() {
return homeTown;
}
// set home town
public void setHomeTown(String homeTown) {
this.homeTown = homeTown;
}
// return address
public List<Address> getAddress() {
return address;
}
// set address
public void setAddress(List<Address> address)
{
this.address = address;
}
/*
* toString method to get the information of
person
*/
@Override
public String toString() {
String s = "";
for (Address address2 : address)
{
s += address2
+ "\t";
}
return "Name: " + firstName + " " +
lastName + "\nAge: " + age + " Years\nHome Town: " + homeTown +
"\nAddress: "
+ s;
}
}// end class Person
/***********************************Address.java************************************/
package app;
/**
* @author
*
* class Address
*/
public class Address {
/*
* Instance variables to store the Address
*/
private String streetAddress;
private String city;
private String country;
private int postalCode;
private AddressType type;
/**
* Parameterized constructor to initialize instance
variables to given
* parameters
*
* @param streetAddress
* @param city
* @param country
* @param postalCode
* @param type
*/
public Address(String streetAddress, String city,
String country, int postalCode, AddressType type) {
this.streetAddress =
streetAddress;
this.city = city;
this.country = country;
this.postalCode = postalCode;
this.type = type;
}
// return street address
public String getStreetAddress() {
return streetAddress;
}
// set street address
public void setStreetAddress(String streetAddress)
{
this.streetAddress =
streetAddress;
}
// return city
public String getCity() {
return city;
}
// set city
public void setCity(String city) {
this.city = city;
}
// return country
public String getCountry() {
return country;
}
// set city
public void setCountry(String country) {
this.country = country;
}
// return postal code
public int getPostalCode() {
return postalCode;
}
// set postal code
public void setPostalCode(int postalCode) {
this.postalCode = postalCode;
}
// get Address type
public AddressType getType() {
return type;
}
// set address type
public void setType(AddressType type) {
this.type = type;
}
@Override
public String toString() {
return streetAddress + ", " + city
+ ", " + country + ", " + postalCode + ", Address type: " +
type.toString();
}
}// end class Address
/*******************************************AddressType.java*****************************/
package app;
/**
* @author
*
* enum AddressType
*/
public enum AddressType {
LOCAL, PERMANENT
}
/***************************************Student.java*****************************/
package app;
import java.util.List;
/**
* @author
*
* class Student
*/
public class Student extends Person {
private String id;
private String major;
private double gpa;
/**
* Constructor to initialize variables
*
* @param firstName
* @param lastName
* @param age
* @param homeTown
* @param address
* @param id
* @param major
* @param gpa
*/
public Student(String firstName, String lastName, int
age, String homeTown, List<Address> address, String id,
String major,
double gpa) {
super(firstName, lastName, age,
homeTown, address);
this.id = id;
this.major = major;
this.gpa = gpa;
}
// return id
public String getId() {
return id;
}
// set id
public void setId(String id) {
this.id = id;
}
// return major
public String getMajor() {
return major;
}
// set major
public void setMajor(String major) {
this.major = major;
}
// get gpa
public double getGpa() {
return gpa;
}
// set gpa
public void setGpa(double gpa) {
this.gpa = gpa;
}
@Override
public String toString() {
return super.toString() +
"\nStudnet ID: " + id + "\nMajor: " + major + "\nGPA: " + gpa;
}
}// end class Student
/***************************************StudentAthlete.java********************************/
package app;
import java.util.List;
import java.util.Random;
/**
* @author
*
* class StudentAthlete
*/
public class StudentAthlete extends Student {
/*
* private data field
*/
private String sports;
private int ranking;
/**
* Constructor to initialize object with these
parameters
*
* @param firstName
* @param lastName
* @param age
* @param homeTown
* @param address
* @param id
* @param major
* @param gpa
* @param sports
* @param ranking
*/
public StudentAthlete(String firstName, String
lastName, int age, String homeTown, List<Address> address,
String id,
String major,
double gpa, String sports) {
super(firstName, lastName, age,
homeTown, address, id, major, gpa);
this.sports = sports;
this.ranking = new
Random().nextInt(100);
}
// return sport name
public String getSports() {
return sports;
}
// set sport name
public void setSports(String sports) {
this.sports = sports;
}
// return ranking
public int getRanking() {
return ranking;
}
// set ranking
public void setRanking(int ranking) {
this.ranking = ranking;
}
/*
* toString method to get the information of Athlete
Student
*/
@Override
public String toString() {
return super.toString() +
"\nSports: " + sports + "\nRanking: " + ranking;
}
}// end class Athlete Student
/***************************************App.java*****************************/
package app;
import java.util.ArrayList;
import java.util.List;
/**
* @author
*
* class main App
*/
public class App {
public static void main(String[] args) {
// create some address object
and add to list
List<Address> address = new
ArrayList<Address>();
address.add(new Address("B-84,Bajaj
Nagar", "Jaipur", "India", 302015, AddressType.LOCAL));
address.add(new Address("MD",
"SWM", "INDIA", 322028, AddressType.PERMANENT));
// create person object
Person p1 = new Student("Ajay",
"Saini", 27, "Jaipur", address, "ST1234", "CSE", 8.8);
// create some address object
for other person and add to list
List<Address> address1 = new
ArrayList<Address>();
address1.add(new Address("Red fort
street", "Delhi", "India", 302015, AddressType.LOCAL));
address1.add(new Address("Sawai
Madhopur", "SWM", "INDIA", 322028, AddressType.PERMANENT));
// create person object
Person p2 = new
StudentAthlete("Virat", "Kohli", 33, "SWM", address1, "ST2344",
"ECE", 5.6, "Football");
// print details of object we
created
System.out.println("Person 1
details: ");
System.out.println(p1.toString());
System.out.println("\nPerson 2
details: ");
System.out.println(p2.toString());
}
}// end class App
/*****************************output*********************************/
Person 1 details:
Name: Ajay Saini
Age: 27 Years
Home Town: Jaipur
Address: B-84,Bajaj Nagar, Jaipur, India, 302015, Address type:
LOCAL MD, SWM, INDIA, 322028, Address type:
PERMANENT
Studnet ID: ST1234
Major: CSE
GPA: 8.8
Person 2 details:
Name: Virat Kohli
Age: 33 Years
Home Town: SWM
Address: Red fort street, Delhi, India, 302015, Address type:
LOCAL Sawai Madhopur, SWM, INDIA, 322028, Address type:
PERMANENT
Studnet ID: ST2344
Major: ECE
GPA: 5.6
Sports: Football
Ranking: 27
PLEASE GIVE ME THUMBUP.......