STEP 1: Understand the UML Class Diagram Use the following UML diagram to build
ID: 3857011 • Letter: S
Question
STEP 1: Understand the UML Class Diagram
Use the following UML diagram to build the class. The first section specifies the attributes. The second section specifies the behaviors, and the first character specifies the access modifier value, where:
"-" means that the class member is private, and
"+" means that the class member is public.
STEP 2: Code the Employee Class
Create a new project called "CIS247B_Week2Lab_YourName".
Using the provided Class Diagram from Step 1, code the Employee class in the new project (i.e., "Realize the UML Class diagrams").
The default constructor should set the attributes as follows: firstName = "not given", lastName = "not given", gender = "U" (for unknown), dependents = 0, and annualSalary = 20,000.
The multi-arg constructor should initialize all of the attributes using values passed in using its parameter list.
As shown in the Class diagram, each attribute should have a "getter" to retrieve the stored attribute value, and a "setter" that modifies the value.
The calculatePay( ) method of the Employee class should return the value of annual salary divided by 52 (return annualSalary / 52;).
The displayEmployee() method should display all the attributes of the Employee object in a well-formatted string with logical labels applied to each attribute. Don't forget to call calculatePay from within the displayEmployee method in order to display the Employee's weekly pay as well!
STEP 3: Code the Main Program
In the Main class, create code statements that perform the following operations. Be sure you follow proper commenting and programming styles (header, indentation, line spacing, etc.).
Create an Employee object using the default constructor.
Prompt for and then set the first name, last name, gender, dependents, and annual salary. (Remember that you have to convert gender, dependents, and annual salary from strings to the appropriate data type.)
Using your code from last week, display a divider that contains the string "Employee Information"
Display the Employee information.
Create a second Employee object using the multi-argument constructor, setting each of the attributes with appropriate valid values.
Using your code from last week, display a divider that contains the string "Employee Information".
Display the Employee information for the second Employee object.
STEP 4: Compile and Test
When done, compile and run your code.
Then, debug any errors until your code is error-free.
Check your output to ensure that you have the desired output, modify your code as necessary, and rebuild.
STEP 5: Screen Prints
Capture the Console output window and paste into a Word document. The following is a sample screen.
Explanation / Answer
import java.util.Scanner;
public class CodeFromPseudo {
public static void main(String[] args) {
Scanner scan = new Scanner(
CodeFromPseudo.class.getResourceAsStream("CarUML.txt"));
//Scanner scan = new Scanner(CodeFromPseudo.class.getResourceAsStream("PetUML.txt"));
String line;
String [] words;
while(scan.hasNextLine()){
line = scan.next();
System.out.printf("public class %s { ",line);
scan.nextLine();
scan.nextLine();
for (int i = 0; i < 4; i++) {
words = scan.nextLine().split("\s+");
System.out.print((words[0].contains("-")) ? "private" : "public");
System.out.printf(" %s %s; ", words[3],words[1]);
}
scan.nextLine();
words = scan.nextLine().split("\s+");
System.out.print((words[0].contains("-")) ? "private " : "public ");
System.out.printf("%s (%s %s) { ",words[1],words[3],words[4]);
System.out.printf("this.%s = %s; ",words[4],words[4]);
System.out.println("} }");
scan.nextLine();
}
}
}
import javax.swing.*;
import java.awt.*;
public class PaySlip extends JFrame
{
JTextField jTextField1;
JTextField jTextField2;
JTextField jTextField3;
JTextField jTextField4;
public PaySlip()
{
JLabel jLabel1 = new JLabel(” Employee Detail “);
JLabel jLabel2 = new JLabel(“Name:- “);
JLabel jLabel3 = new JLabel(“Basic Salary :-”);
JLabel jLabel4 = new JLabel(“Allowance in %”);
JLabel jLabel5 = new JLabel(“Deduction in %”);
jTextField1 = new JTextField(10);
jTextField2 = new JTextField(10);
jTextField3 = new JTextField(10);
jTextField4 = new JTextField(10);
JButton jButton1 = new JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jButton1.setText(“Calculate”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
Container contentPane = getContentPane();
JPanel p=new JPanel();
p.add(jLabel1);
p.add(jLabel2);
p.add(jTextField1);
p.add(jLabel3);
p.add(jTextField2);
p.add(jLabel4);
p.add(jTextField3);
p.add(jLabel5);
p.add(jTextField4);
p.add(jButton1);
contentPane.add(p);
}
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name1=jTextField1.getText();
int basicsal=Integer.parseInt(jTextField2.getText());
int allow1=Integer.parseInt(jTextField3.getText());
int deduct1=Integer.parseInt(jTextField4.getText());
PaySlip1 calfr=new PaySlip1(name1,basicsal,allow1,deduct1);
calfr.setVisible(true);
}
public static void main(String args[])
{
PaySlip ps= new PaySlip();
ps.setVisible(true);
ps.setSize(230,220);
ps.setTitle(“Emplyee Details”);
ps.setResizable(false);
}
}
PaySlip1.java
import javax.swing.*;
import java.awt.*;
public class PaySlip1 extends javax.swing.JFrame
{
public PaySlip1() { }
public PaySlip1(String name1,int basicsal,int allows1 , int deduct1)
{
JLabel jLabel1 = new JLabel(” Payment Slip “);
JLabel jLabel2 = new JLabel(” Name :- “);
JTextField jTextField1 = new JTextField(10);
JLabel jLabel3 = new JLabel(“basic :-”);
JTextField jTextField2 = new JTextField(10);
JLabel jLabel4 = new JLabel(“Allowance”);
JTextField jTextField3 = new JTextField(10);
JLabel jLabel5 = new JLabel(“Deduction”);
JTextField jTextField4 = new JTextField(10);
JLabel jLabel6 = new JLabel(“Net Salary :-”);
JTextField jTextField5 = new JTextField(10);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jTextField1.setEditable(false);
jTextField2.setEditable(false);
jTextField3.setEditable(false);
jTextField4.setEditable(false);
jTextField5.setEditable(false);
Container contentPane = getContentPane();
JPanel p=new JPanel();
p.add(jLabel1);
p.add(jLabel2);
p.add(jTextField1);
p.add(jLabel3);
p.add(jTextField2);
p.add(jLabel4);
p.add(jTextField3);
p.add(jLabel5);
p.add(jTextField4);
p.add(jLabel6);
p.add(jTextField5);
contentPane.add(p);
setVisible(true);
setSize(230,220);
setTitle(“Payment Slip”);
setResizable(false);
jTextField1.setText(name1);
jTextField2.setText(“”+basicsal);
int allows2=((basicsal*allows1)/100);
jTextField3.setText(“”+allows2);
int deduct2=((basicsal*deduct1)/100);
jTextField4.setText(“”+deduct2);
jTextField5.setText(“”+(basicsal+allows2-deduct2));
}
}
import java.util.Scanner;
class Employee
{
int age;
String name, address, gender;
Scanner get = new Scanner(System.in);
Employee()
{
System.out.println("Enter Name of the Employee:");
name = get.nextLine();
System.out.println("Enter Gender of the Employee:");
gender = get.nextLine();
System.out.println("Enter Address of the Employee:");
address = get.nextLine();
System.out.println("Enter Age:");
age = get.nextInt();
}
void display()
{
System.out.println("Employee Name: "+name);
System.out.println("Age: "+age);
System.out.println("Gender: "+gender);
System.out.println("Address: "+address);
}
}
class fullTimeEmployees extends Employee
{
float salary;
int des;
fullTimeEmployee()
{
System.out.println("Enter Designation:");
des = get.nextInt();
System.out.println("Enter Salary:");
salary = get.nextFloat();
}
void display()
{
System.out.println("=============================="+" "+"Full Time Employee Details"+" "+"=============================="+" ");
super.display();
System.out.println("Salary: "+salary);
System.out.println("Designation: "+des);
}
}
class partTimeEmployees extends Employee
{
int workinghrs, rate;
partTimeEmployees()
{
System.out.println("Enter Number of Working Hours:");
workinghrs = get.nextInt();
}
void calculatepay()
{
rate = 8 * workinghrs;
}
void display()
{
System.out.println("=============================="+" "+"Part Time Employee Details"+" "+"=============================="+" ");
super.display();
System.out.println("Number of Working Hours: "+workinghrs);
System.out.println("Salary for "+workinghrs+" working hours is: $"+rate);
}
}
class Employees
{
public static void main(String args[])
{
System.out.println("================================"+" "+"Enter Full Time Employee Details"+" "+"================================"+" ");
fullTimeEmployees ob1 = new fullTimeEmployees();
partTimeEmployees ob = new partTimeEmployees();
System.out.println("================================"+" "+"Enter Part Time Employee Details"+" "+"================================"+" ");
ob1.display();
ob.calculatepay();
ob.display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.