I need help on this homework/lab - java

I finished all the classes except for the student class. I don't understand how to approach it. Here is the prompt.
Extend the Person class developed in
lab1 to derive classes for students,
hourly employees, and full-time
salaried employees. Determine an
appropriate class inheritance
hierarchy. These classes should have
the following fields, necessary
constructors, and appropriate access
and modifier methods.
for all employees:
*department
full-time employees:
*salary
hourly employees:
*hourly rate
*number of hours worked each week(4weeks)
the hourly employee class should
contain the necessary methods that
will print the total hours (four- week
total), average hours per week worked
by each employee, and the total wages
during a four-week period.
student:
*classes taken and grades for each class (use an ArrayList)
The student class should contain the
necessary methods to print the
transcript for each student
(write a tester class to test your
classes)
How should I use an arraylist for the student class?
I'm only going to post the relevant classes
public class Person { private String first; private String last; private static int idNumber = 1001; int Id ; private String full;
Person(){
}
Person(String fn,String ln){
first = fn;
last = ln;
Id = idNumber++;
full = first +" "+ last;
}
static int getidNumber(){
return idNumber;
}
void setfirst(String fn) {
first = fn;
}
String getfirst(){
return first; }
void setlast(String ln){
last = ln; }
String getlast(){
return last; }
#Override
public String toString(){
String blah = "First name: " +first+ " Last Name:" +last+ "\tThe full name is: "+full+" Id#"+Id;
return blah;
}
}
import java.util.*;
public class Student extends Person{
Student (String fn, String ln){
super(fn,ln);
}
}
Thank you in advance for all advices and suggestions.

I would use a map here, but since an ArrayList is required, I would do something like that:
public class Student extends Person {
private List<ClassGrade> classes = new ArrayList<ClassGrade>();
public List<ClassGrade> getClassGrades() {
return new ArrayList<ClassGrade>(classes);
}
public void addClass(String clazz, int grade) {
classes.add(new ClassGrade(clazz, grade));
}
public static class ClassGrade {
String clazz;
int grade;
public ClassGrade(String clazz, int grade) {
this.clazz = clazz;
this.grade = grade;
}
public String getClazz() {
return clazz;
}
public int getGrade() {
return grade;
}
}
}

import static java.lang.System.*;
class Student extends Person
{
protected int avMark;
String classTaken
public Student()
{
}
public Student(String nameInput, int ageInput, String class)
{
super(nameInput, ageInput);
classTaken = class;
}
public void setClasTaken(String className){
classTaken = classname;
}
public String setClasTaken(){
return classTaken;
}
public void register()
{
super.register();
out.println("Classes taken " +classTaken
out.println("Average mark is " + avMark);
}
}
here is something to start you off
this class inherits everything from the person class.

Related

Inheritance and arrays in Java

I have a class look like this:
public class People {
private String Name;
private String Address;
public People(String aName, String aAddress) {
this.Name=aName;
this.Address=aAddress;
}
public String getName() {
return Name;
}
public String getAddress() {
return Address;
}
void display() {
System.out.println("Name:\t"+Name);
System.out.println("Address:\t" +Address);
}
}
and another 2:
class Students extends People{
private int MatriculationNumber;
private String CourseName;
public Students (String aName, String aAddress, int matriculationNumber, String courseName){
super(aName,aAddress);
this.MatriculationNumber=matriculationNumber;
this.CourseName=courseName;
}
void display() {
super.display();
System.out.println("Matriculation Number: \t" +MatriculationNumber);
System.out.println("Course Name: \t" +CourseName);
}
}
class Staffs extends People{
private int EmployeeNumber;
private String Department;
public Staffs (String aName, String aAddress, int employeeNumber, String department){
super(aName,aAddress);
this.EmployeeNumber=employeeNumber;
this.Department=department;
}
void display() {
super.display();
System.out.println("Employee Number: \t" +EmployeeNumber);
System.out.println("Department: \t" +Department);
}
}
The question is how to create a class named "School" which have a List can contain both Students and Staffs, so I can add a method like AddPeople() which can add or remove Students or Staffs from it?
You can use the below solution. The solution is not thread-safe but should work fine for your usecase.
import java.util.ArrayList;
class School {
private List<People> peopleList = new ArrayList<People>();
public void addPeople(People people) {
peopleList.add(people);
}
public void removePeople(People people) {
peopleList.remove(people);
}
public static void main(String... args) {
Student student = new Student("Name", "Address", 90, "Course");
Staff staff = new Staff("Name", "Address", 1001, "Department");
School school = new School();
school.addPeople(student);
school.addPeople(staff);
school.removePeople(student);
school.removePeople(staff);
}
}
You can create a new Staff Class which also extends from the People Class
package so;
public class Staff extends People {
public Staff(String aName,String aAddress) {
super(aName, aAddress);
}
}
Then give as type of your list the base class People.
package so;
import java.util.ArrayList;
import java.util.List;
public class School {
List<People> peopleContainer = new ArrayList<People>();
public void addPeople(People p){
this.peopleContainer.add(p);
}
public void removePeople(People p) {
this.peopleContainer.remove(p);
}
public void displayPeople() {
for(People person : peopleContainer) {
person.display();
}
}
}
Then you can add staff and student objects to your list and also all other objects which classes extends from people or even people objects itself.
package so;
public class Main {
public static void main(String[] args) {
Students stud1 = new Students("Max", "MustermanAddress", 181242, "CS50");
Staff staff1 = new Staff("Christian", "AugustAddress");
School school = new School();
school.addPeople(stud1);
school.addPeople(staff1);
school.displayPeople();
school.removePeople(staff1);
System.out.println();
System.out.println();
school.displayPeople();
}
}
Output will be
Name: Max
Address: MustermanAddress
Matriculation Number: 181242
Course Name: CS50
Name: Christian
Address: AugustAddress
Name: Max
Address: MustermanAddress
Matriculation Number: 181242
Course Name: CS50

Adding elements to an ArrayList from another class

I just have this basic code where I need help adding employee data to an ArrayList of another class. I am just writing this code in preparation for an assignment, so don't bash my code too much. Essentially though, i'll be needing to add elements of employees and delete them eventually. But for now, I just need help adding the elements to my other Employee class. =]
public class main {
private static Employee employee;
public static void main(String[] args) {
employee = new Employee(10,10);
System.out.println(employee.toString());
}
}
...............
import java.util.ArrayList;
public class Employee {
public int employeeNum;
public double hourRate;
ArrayList<Employee> Employee = new ArrayList<>();
public Employee(int employeeNum, double hourRate){
this.employeeNum = employeeNum;
this.hourRate = hourRate;
}
public String toString(){
return ""+employeeNum+hourRate;
}
}
Simple Example -
package com;
import java.util.ArrayList;
public class TestPage{
public static void main(String[] args){
Employee emp1, emp2;
emp1 = new Employee();
emp2 = new Employee();
emp1.setName("MAK");
emp2.setName("MICHELE");
emp1.setAddress("NY");
emp2.setAddress("WY");
//and keep putting other information like this
ArrayList<Employee> employee = new ArrayList<Employee>();
employee.add(emp1);
employee.add(emp2);
System.out.println("emp1 name is : " + employee.get(0).getName());
System.out.println("emp2 name is : " + employee.get(1).getName());
System.out.println("emp1 address is : " + employee.get(0).getAddress());
System.out.println("emp2 address is : " + employee.get(1).getAddress());
}
}
class Employee{
String name, address;
int age, salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
It seems like what you're asking is based on one employee having sub-employees and that structurally that probably represents a hierarchy (Some commenters seem to be missing that point). But that's an assumption on my part. Based on that assumption.
A little bit of feedback to start on structure of your main class:
public class main {
public static void main(String[] args) {
Employee employee = new Employee(10,10);
System.out.println(employee.toString());
}
}
It seems to me that there's no reason to have a static instance variable for that root employee instance. You should try to limit the scope of variables where possible. It seems like it could very well be in the main() method's scope.
public class Employee {
public int employeeNum;
public double hourRate;
ArrayList<Employee> employees= new ArrayList<>();
public Employee(int employeeNum, double hourRate){
this.employeeNum = employeeNum;
this.hourRate = hourRate;
}
public String toString(){
return ""+employeeNum+hourRate;
}
public ArrayList<Employee> getEmployees() {
return this.employees;
}
}
It may be better to name your arraylist employees or employeeList. I went with employees in this case because that convention is preferable.
And in relation to your question, ArrayList is pass by reference so you could just add a getter method for the sub-employee list (employees).
To add employees from your main method you could do something like
Employee rootEmployee = new Employee(5, 10.0);
rootEmployee.getEmployees().add(new Employee(6, 5.0));
Or you could add an additional method to Employee like this:
public void addEmployee(Employee e) {
employees.add(e);
}

interface,abstract and classes

I am trying to complete the Appendix attached Appendix. I just want to know if I am coding it correctly according to the Appendix and that I am using the correct approach. I am not sure if I did the correct thing under interest(). Where I called the super classes is that correct?
public interface LoanInterest {
double interest();
String getName();
String toString();
} //end of LoanInterest
public abstract class Student implements LoanInterest {
private String name;
private String studentNumber;
private double feesOwed;
public Student(String nm, String num, double amt) {
name = nm;
studentNumber = num;
feesOwed = amt;
}
public double getFeesOwed() {
return feesOwed;
}
public String getStudentNumber() {
return studentNumber;
}
public String getName() {
return name;
}
public String toString() {
String msg;
msg = String.format("%s\t%s\t%.2f", name, getStudentNumber(), getFeesOwed());
return msg;
}
} //end of Student
public class UnderGrad extends Student {
public UnderGrad(String nm, String num, double amt) {
super(nm, num, amt);
}
public double interest() {
return super.getFeesOwed() + (super.getFeesOwed() * 0.14);
}
} //end of UnderGrad
public class PostGrad extends Student {
private String diploma;
public PostGrad(String nm, String num, double amt) {
super(nm, num, amt);
}
public String getDiploma() {
return diploma;
}
public double interest() {
return super.getFeesOwed() + (super.getFeesOwed() * 0.14);
}
} //end of PostGrad
You don't need to call super.methodName, since you do not override them in PostGrad or UnderGrad, but it is not "wrong" either.
So you can write:
public double interest() {
return getFeesOwed() + (getFeesOwed() * 0.14);
}
And if you would override them, you most likely want to use them too, so again no super.
The super keyword is normally used, if a method is overridden to add some additional functionality to it, without completely rewriting the code of the overridden method.

Java class setter not setting correct value to class field.

I have a simple java program I and I cannot figure out why the setter for a class will not set the correct value.
I have a class Employee, a class Department, a class Company. Once I am able to set correct values to the fields of an Employee instance I will then store that employee in a arraylist of employees in an instance of Department(arrayList field).
The class called Employee. It has four fields, String fName, String lName, int age, String department. I am able to set fName and lName though age is always set to 0 and department is always set to null.
Here is the code for the employee class:
public class Employee {
private String fName;
private String lName;
private String department;
private int age;
//getters and setters for the private fields of the Employee class
public void setAge(int num){
num = age;
}
public int getAge(){
return age;
}
public void setDepartment(String dep){
dep = department;
}
public String getDepartment(){
return department;
}
public void setfName(String afName){
fName = afName;
}
public String getfName(){
return fName;
}
public void setlName(String alName){
lName = alName;
}
public String getlName(){
return lName;
}
}
Here is the code for a method called addEmployee:
public void AddEmployee(Department depInstance){
String firstName = JOptionPane.showInputDialog("Enter employee First name");
String lastName = JOptionPane.showInputDialog("Enter employee last name");
int empAge = Integer.parseInt(JOptionPane.showInputDialog("Enter employee age"));
String empDep = JOptionPane.showInputDialog("Enter employee department");
Employee employeeToAdd = new Employee();
employeeToAdd.setfName(firstName);
employeeToAdd.setlName(lastName);
employeeToAdd.setAge(empAge);
employeeToAdd.setDepartment(empDep);
//test input and variable setting
System.out.println("--------Inputs------");
varTester(firstName,lastName,empAge,empDep);
System.out.println("--------Recorded Vals------");
varTester(employeeToAdd.getfName(), employeeToAdd.getlName(),employeeToAdd.getAge(),employeeToAdd.getDepartment());
public static void varTester(String empfName, String emplName, int empAge, String empDep){
System.out.println(empfName);
System.out.println(emplName);
System.out.println(empAge);
System.out.println(empDep);
}
}
This is the output from the test method varTester():
--------Inputs------
Somefirstname
Somelastname
32
Accounting
--------Recorded Vals------
Somefirstname
Somelastname
0
null
I test the values received from the showInputDialog's and it is the correct values I want to store int the class instance fields of employeeToAdd though only the first and last name values are set and not the age or department. Can someone point me in the right direction. Thank you.
You got the setter backwards. It should be :
public void setAge(int num){
age = num;
}
You have the same error in setDepartment.
You are supposed to assign to the member variable, not to the argument of the setter method.
Your setter sets the argument not the private field.
public void setAge(int num){
num = age;
}
public void setDepartment(String dep){
dep = department;
}
Change it to:
public void setAge(int num){
age = num;
}
public void setDepartment(String dep){
department = dep;
}
It should be:
public void setAge(int num){
age = num;
}
public void setDepartment(String dep){
department = dep;
}

How to create a roster of students in java

I have a homework assignment problem that looks like this:
(20 pts) Create a Student class with the following:
A private String variable named “name” to store the student’s name
A private integer variable named “UFID” that contains the unique ID number for this student
A private String variable named “DOB” to store the student’s date of birth
A private integer class variable named numberOfStudents that keeps track of the number of students that have been created so far
A public constructor Student(String name, int UFID, String dob)
Several public get/set methods for all the properties
getName/setName
getUFID/setUFID
getDob/setDob
Write a test program, roster.java, that keeps a list of current enrolled students. It should have methods to be able to enroll a new
student and drop an existing student.
I'm not asking anyone to do this assignment for me, I just really need some general guidance. I think I have the Student class pretty well made, but I can't tell exactly what the addStudent() and dropStudent() methods should do - should it add an element to an array or something or just increments the number of students? The code I have so far looks like this.
public class Student {
private String name;
private int UFID;
private String DOB;
private static int numberOfStudents;
public Student(String name, int UFID, String DOB) {
this.name = name;
this.UFID = UFID;
this.DOB = DOB;
}
public String getDOB() {
return DOB;
}
public void setDOB(String dOB) {
DOB = dOB;
}
public int getUFID() {
return UFID; }
public void setUFID(int uFID) {
UFID = uFID; }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public void setNumberOfStudents(int numberOfStudents) {
Student.numberOfStudents = numberOfStudents;
}
public static void addStudent(String name, int UFID, String DOB) {
numberOfStudents++;
}
public static void dropStudent(String name) {
numberOfStudents--;
}
}
Any guidance as I finish this up would be greatly appreciated.
The assignment writes itself: you need a Roster class that owns and maintains a collection of Students:
public class Roster {
private Set<Student> roster = new HashSet<Student>();
public void addStudent(Student s) { this.roster.add(s); }
public void removeStudent(Student s) { this.roster.remove(s); }
}

Categories