Create a Java application using the UML diagram. You cannot use the same Employe
ID: 3590188 • Letter: C
Question
Create a Java application using the UML diagram. You cannot use the same Employee example.Create a class of your own. It should have at least four data members/attributes and five methods.
Example:
public class CreateEmployeeObjects
{
public static void main (String[] args)
{
int mnum;
Employee manager = new Employee();
Employee staff = new Employee();
Employee clerk = new Employee();
manager.setempNum(543);
clerk.setempNum(84);
mnum = manager.getempNum();
System.out.println ("The manager obeject has the empployee Number: " + mnum);
staff.setempNum(52);
System.out.println ("The staff obeject has the empployee Number: " + staff.getempNum());
System.out.println ("The clerk obeject has the empployee Number: " + clerk.getempNum());
}
}
Example 2
public class Employee
{
private int empNum;
private String empFName;
private String empLName;
private double empSalary;
public int getempNum()
{
return empNum;
}
public void setempNum (int num)
{
empNum = num;
}
public String getempFName()
{
return empFName;
}
public void setempFName(String fname)
{
empFName = fname;
}
public String getempLName()
{
return empLName;
}
public void setempLName(String lname)
{
empLName = lname;
}
public double getEmpSalary()
{
return empSalary;
}
public void setEmpSalary(double sal)
{
empSalary = sal;
}
}
/*
public clss newemployee()
{
public static void main (String [] args)
{
}
}
*/
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
________________
Student.java
public class Student {
//Declaring instance variables
private int id;
private String name;
private String course;
private String collegeName;
//Parameterized constructor
public Student(int id, String name, String course, String collegeName) {
super();
this.id = id;
this.name = name;
this.course = course;
this.collegeName = collegeName;
}
//getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Id=" + id + ", Name=" + name + ", Course=" + course + ", College Name=" + collegeName;
}
}
_________________
StudentDemo.java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) {
int id;
String name, course, collegeName;
ArrayList < Student > arl = new ArrayList < Student > ();
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
// Getting the input entered by the user
for (int i = 0; i < 3; i++) {
System.out.println("Student#" + (i + 1) + ":");
System.out.print("Enter Id :");
id = sc.nextInt();
sc.nextLine();
System.out.print("Enter Name :");
name = sc.nextLine();
System.out.print("Enter Course :");
course = sc.nextLine();
System.out.print("Enter College Name :");
collegeName = sc.nextLine();
// Creating an Student class object by passing the user entered
// inputs as arguments
Student s = new Student(id, name, course, collegeName);
// Adding the Student to the arrayList
arl.add(s);
}
// Displaying all students info
System.out.println("--- Displaying all Students Info ---");
for (int i = 0; i < arl.size(); i++) {
System.out.println("Student#" + (i + 1) + ":");
System.out.println(arl.get(i).toString());
}
// Modifying the student 1 info by Calling the setters
System.out.println("--- Calling the Setters on the Student#1 ---");
arl.get(0).setName("Williams");
arl.get(0).setCollegeName("St Xaviers College");
arl.get(0).setCourse("Mechanical Engineering");
// Displaying all students info
System.out.println("--- Displaying all Students Info ---");
for (int i = 0; i < arl.size(); i++) {
System.out.println("Student#" + (i + 1) + ":");
System.out.println(arl.get(i).toString());
}
}
}
__________________
Output:
Student#1:
Enter Id :1234
Enter Name :James Thomson
Enter Course :Computer Science
Enter College Name :Sheffield Hallam Uiversity
Student#2:
Enter Id :4567
Enter Name :Sachin Meley
Enter Course :Compter Science
Enter College Name :Leeds University
Student#3:
Enter Id :5434
Enter Name :Ricky Pant
Enter Course :Electrical Engineering
Enter College Name :oxford University
--- Displaying all Students Info ---
Student#1:
Id=1234, Name=James Thomson, Course=Computer Science, College Name=Sheffield Hallam Uiversity
Student#2:
Id=4567, Name=Sachin Meley, Course=Compter Science, College Name=Leeds University
Student#3:
Id=5434, Name=Ricky Pant, Course=Electrical Engineering, College Name=oxford University
--- Calling the Setters on the Student#1 ---
--- Displaying all Students Info ---
Student#1:
Id=1234, Name=Williams, Course=Mechanical Engineering, College Name=St Xaviers College
Student#2:
Id=4567, Name=Sachin Meley, Course=Compter Science, College Name=Leeds University
Student#3:
Id=5434, Name=Ricky Pant, Course=Electrical Engineering, College Name=oxford University
____________Thank You