I need help creating a Program
Must Include:
1. Student
a. Must have
i. Student name (first and last)
ii. Birthdate
iii. ID #
iv. Grade (Freshman, sophomore, etc.)
v. Class Schedule (an array?)
vi. 3 constructors of your choice (must include a default)
vii. Getter and Setter methods for all instance variables
1. This means that you have at least 2 methods for each instance variable in order to be able to get and set those variables.
2. The methods will be public, so that they can be used outside the class, and the instance variables will be private, so that they are not broken by the users of your class.
viii. Proper documentation for all methods
This is what i have so far
public class StudentTester
{
public static void main(String[] args)
{
Student someGuy = new Student(); //This creates a new student using the defalt constructor
Student someGal = new Student("Amanda","Soh"); //This creates a new student and automatically sets the firt and last name variables
String justAHolder = someGuy.getFullName();
String justAnotherHolder = someGal.getFullName();
System.out.print(someGal.getFullName());
someGuy.setMonth(11);
someGal.setMonth(14);
//If you uncomment the next line and try and compile the program, you will be able to see how it generates an error
//someGal.grade=12; //NO, NO, No, grade is supposed to be private!!!!!
}
/**
* Constructor for objects of class Student
*/
public Student()
{
// initialise instance variables
fName = "John";
lName = "Doe";
}
/**
* This is my second constructor example that students name
*
* #param fName This is the input for the first name
* #param lName This is the input for the last name
*/
public Student(String fName, String lName)
{
this();
this.fName = fName;
this.lName = lName;
}
//Here are the Getter Methods
/**
* This is the getter method to get the full name
*
* #return This returns the first and last name as a single String
*/
public String getFullName()
{
return fName+" "+lName;
}
//Here are the Setter Methods
/**
* This is the method to set the month of the students birthday
*
* #param newMonth The new month to set the birthdate with
*/
public void setMonth(int newMonth)
{
if(newMonth>0 && newMonth<13)
{
bDayMonth = newMonth;
}else
{
System.out.println("No one is born in Octovember!");
}
}
}
Your Student class :
package com.test;
public class Student {
public Student() {
firstName = "John";
lastName = "Doe";
birthDate = "05/11/1990";
}
public Student(String fName, String lName, String dob, String sGrade,
String[] classSchedule) {
firstName = fName;
lastName = lName;
birthDate = dob;
grade = sGrade;
schedule = classSchedule;
}
private String firstName;
private String lastName;
private String birthDate;
private String grade;
private String[] schedule;
/**
* #return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* #param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* #return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* #param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* #return the birthDate
*/
public String getBirthDate() {
return birthDate;
}
/**
* #param birthDate
* the birthDate to set
*/
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
/**
* #return the grade
*/
public String getGrade() {
return grade;
}
/**
* #param grade
* the grade to set
*/
public void setGrade(String grade) {
this.grade = grade;
}
/**
* #return the schedule
*/
public String[] getSchedule() {
return schedule;
}
/**
* #param schedule
* the schedule to set
*/
public void setSchedule(String[] schedule) {
this.schedule = schedule;
}
public String getFullName() {
return firstName + " " + lastName;
}
}
Then the tester class:
package com.test;
public class StudentTester {
/**
* #param args
*/
public static void main(String[] args) {
Student st1 = new Student(); // This creates a new student using the
// defalt constructor
String[] sch = { "Math 10.30", "Physics 14.00" };
Student st2 = new Student("Amanda", "Soh", "10/12/1990", "Freshman",
sch);// This creates a new student and automatically sets all
// attributes
System.out.println("First Student is: " + st1.getFullName());
System.out.println("Details of 2nd student: " + st2.getFullName() + " "
+ st2.getBirthDate() + " " + st2.getGrade() + " "
+ st2.getSchedule()[0] + " " + st2.getSchedule()[1]);
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package addressname;
/**
*
* #author MY PC
*/
public class AddressName {
public static void main(String[] arg){
}
private String name;
private String streetAddress;
private String city;
private String state;
private String zipCode;
/**
* Create an address with all empty fields.
*
*/
public AddressName ()
{
name = "";
streetAddress = "";
city = "";
state = "";
zipCode = "";
}
/**
* Create an address.
*/
public AddressName (String nm, String streetAddr, String city,
String state, String zip)
{
name = nm;
streetAddress = streetAddr;
this.city = city;
this.state = state;
zipCode = zip;
}
/**
* #return the theName
*/
public String getName() {
return name;
}
/**
* #param theName the theName to set
*/
public void setName(String theName) {
this.name = theName;
}
/**
* #return the streetAddress
*/
public String getStreetAddress() {
return streetAddress;
}
/**
* #param streetAddress the streetAddress to set
*/
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
/**
* #return the city
*/
public String getCity() {
return city;
}
/**
* #param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* #return the state
*/
public String getState() {
return state;
}
/**
* #param state the state to set
*/
public void setState(String state) {
this.state = state;
}
/**
* #return the zipCode
*/
public String getZipCode() {
return zipCode;
}
/**
* #param zipCode the zipCode to set
*/
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
/**
* True if the names and addresses are equal
*/
public boolean equals (Object right)
{
AddressName r = (AddressName)right;
return name.equals(r.name)
&& streetAddress.equals(r.streetAddress)
&& city.equals(r.city)
&& state.equals(r.state)
&& zipCode.equals(r.zipCode);
}
public int hashCode ()
{
return name.hashCode() + 3 * streetAddress.hashCode()
+ 5 * city.hashCode()
+ 7 * state.hashCode()
+ 11 * zipCode.hashCode();
}
public String toString()
{
return name + ": " + streetAddress + ": "
+ city + ", " + state + " " + zipCode;
}
public Object clone()
{
return new AddressName(name, streetAddress, city,
state, zipCode);
}
}
Output
Complete successful
Modify the body of the main method like this:
public static void main(String[] arg) {
Address address = new Address("The Game", "Seventh street", "Generic Town", "State", "1234");
System.out.println(address.toString());
}
I don't really know if my question makes sense but my assignment says:
"Write a third class, StudentRecord, that has two attributes:
Student stu;
Address addr;
and two constructors. The first constructor is given a Student object and an Address object to initialize the attributes. The second constructor is given a first name, a last name, a student ID, a gpa, a street address, a city, a state, and a zipcode and uses these to initialize the attributes"
I don't understand how exactly I'm supposed to make two constructors take info from two different java files.
Here's the code I have for the third class named "StudentRecord".
I have no doubt it's incorrect.
public class StudentRecord {
Student stu;
Address addr;
public StudentRecord() {
Student stu;
Address addr;
}
public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
}
public String toString() {
return String.format(stu + "\n" + addr);
}
}
Here's the code I have for the TestStudentRecord class, all I get is
"null null" when I run the program.
public class TestStudentRecord {
public static void main(String[] args) {
StudentRecord stu1 = new StudentRecord("Jane", "Brown", 182765, 2.333, "13 Flower St.", "Pulteneyville", "NY", 14386);
System.out.println(stu1);
}
}
All I get is "null null" when I run the program instead of the toString method giving me the student info I have typed into the test class.
For those asking for the Student and Address classes, here you go:
public class Student {
// attributes of a Student
private String firstName;
private String lastName;
private int studentId;
private double gpa;
/**
* Student constructor.
* #param _fName student's first name
* #param _lName student's last name
* #param _id student's id number
* #param _gpa students GPA
*/
public Student(String _fName, String _lName, int _id, double _gpa) {
firstName = _fName;
lastName = _lName;
studentId = _id;
gpa = _gpa;
}
/**
* getFirstName - Accessor for first name
* #return the student's first name
*/
public String getFirstName() {
return firstName;
}
/**
* getLastName - Accessor for last name
* #return the student's last name
*/
public String getLastName() {
return lastName;
}
/**
* getId - Accessor for ID
* #return the student's ID
*/
public int getStudentId() {
return studentId;
}
/**
* getGpa - Accessor for gpa
* #return the student's gpa
*/
public double getGpa() {
return gpa;
}
/**
* setFirstName - Mutator for first name
* #param the new first name
*/
public void setFirstName(String _fName) {
firstName = _fName;
}
/**
* setLastName - Mutator for last name
* #param the new last name
*/
public void setLastName(String _lastName) {
lastName = _lastName;
}
/**
* setStudentId - Mutator for ID
* #param the new ID
*/
public void setStudentId(int _id) {
studentId = _id;
}
/**
* setGpa - Mutator for gpa
* #param the new gpa
*/
public void setGpa(double _gpa) {
gpa = _gpa;
}
// toString Method
public String toString() {
return String.format(getLastName() + ", " + getFirstName() + "\n" + "ID: " + getStudentId() + " GPA: %3.1f", getGpa());
}
}
public class Address {
private String street;
private String city;
private String state;
private int zip;
public Address(String _street, String _city, String _state, int _zip) {
street = _street;
city = _city;
state = _state;
zip = _zip;
}
// Accessors
public String getStreet() {
return street;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public int getZip() {
return zip;
}
// Mutators
public void setStreet(String _street) {
street = _street;
}
public void setCity(String _city) {
city = _city;
}
public void setState(String _state) {
state = _state;
}
public void setZip(int _zip) {
zip = _zip;
}
// toString Method
public String toString() {
return String.format(getStreet() + "\n" + getCity() + ", " + getState() + " " + getZip());
}
}
would suggest you to please follow below approach to initialize your object through argumented construtor.
public class StudentRecord {
Student stu;
Address addr;
public StudentRecord() {
Student stu;
Address addr;
}
public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
this.stu = new Student(_fName, _lName, _id, _gpa);
this.addr=new Address(_street,_city,_state,_zip);
}
public String toString() {
return String.format(stu + "\n" + addr);
}
}
As you have already overided toString method in address and student class so you will get the object.
Your constructor is like below. Empty Constructor will not assign values to your class variables.
public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
}
it should be like below as mentioned by Eran.
public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
this.stu = new Student(_fName, _lName, _id, _gp);
this.addr = new Address(_street, _city, _state, _zip);
}
Now your toString will have the values for stu and addr. Which will get printed.
You have to initilize the variables with this.{variable name} = {variable name}.
Also it is often better to chain the Constructors (see How do I call one constructor from another in Java?), in this way changes in classes, with many constructors, are way easier to implement.
I would also suggsest to rename your variables, because in Java they should not start with an underscore (see https://www.javatpoint.com/java-naming-conventions).
public class StudentRecord {
Student student;
Address addr;
public StudentRecord(Student student, Address addr) {
this.student = student;
this.addr = addr;
}
public StudentRecord(String fName, String lName, int id, double gpa, String street, String city, String state, int zip){
this(new Student(fName, lName, id, gpa),
new Address(street,city,state,zip));
}
public String toString() {
return String.format(student + "\n" + addr);
}
}
I'm trying to write a simple payroll system program. I'm having problems with super(employee_1); in the HourlyEmployee(HourlyEmployee employee_1) method. The reason I'm getting is "actual and formal arguments differ in length. However, when I change the arguments to include everything, I get an error in my main class(payrollSystem_1) in the line HourlyEmployee employee_2 = new HourlyEmployee(employee_1); it says that the method should only say HourlyEmployee(HourlyEmployee employee_1).
I'm just stuck and can't fix the super(employee_1).
PayrollSystem_1.java
package payrollsystem_1;
import java.util.*;
public class PayrollSystem_1 {
public static void main(String[] args) {
// TODO code application logic here
// Creating an HourlyEmployee object using the first constructor
HourlyEmployee employee_1 = new HourlyEmployee(1, "Janette", "Hernandez", null, 14.75, 30);
// Creating an HourlyEmployee object using the copy constructor
HourlyEmployee employee_2 = new HourlyEmployee(employee_1);
// Calling some of the setter methods in the HourlyEmployee class.
employee_2.setEmployeeID(2);
employee_2.setFirstName("Marcela");
employee_2.setLastName("Brown");
employee_2.setHourlyRate(25);
employee_2.setPeriodHours(40);
System.out.println("\nCalling some of the getter methods in the HourlyEmployee class...");
System.out.println( String.format("%-30s%s", "Employee ID:", employee_1.getEmployeeID()) );
System.out.println( String.format("%-30s%s", "First Name:", employee_1.getFirstName()) );
System.out.println( String.format("%-30s%s", "Last Name:", employee_1.getLastName()) );
System.out.println( String.format("%-30s%s", "Hourly Rate:", employee_1.getHourlyRate()) );
System.out.println( String.format("%-30s%s", "Period Hours:", employee_1.getPeriodHours()) );
HourlyEmployee.java
public class HourlyEmployee extends Employee {
private double hourlyRate;
private double periodHours;
/**
* #param employeeID
* #param firstName
* #param lastName
* #param listOfPaychecks
* #param hourlyRate
* #param periodHours
* Constructor with parameters
*/
public HourlyEmployee( int employeeID, String firstName, String lastName, ArrayList<Paycheck> listOfPaychecks,
double hourlyRate, double periodHours) {
super(employeeID, firstName, lastName, listOfPaychecks);
this.hourlyRate = hourlyRate;
this.periodHours = periodHours;
}
/**
* #param employee_1
*
*
*/
public HourlyEmployee(HourlyEmployee employee_1) {
super(employee_1);
this.hourlyRate = employee_1.hourlyRate;
this.periodHours = employee_1.periodHours;
}
/**
* #return hourlyRate
*/
public double getHourlyRate() {
return hourlyRate;
}
/**
* #param hourlyRate
*/
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
/**
* #return periodHours
*/
public double getPeriodHours() {
return periodHours;
}
/**
* #param periodHours
*/
public void setPeriodHours(double periodHours) {
this.periodHours = periodHours;
}
}
Employee.java
public abstract class Employee {
int employeeID;
String firstName;
String lastName;
ArrayList<Paycheck>listOfPaychecks;
double hourlyRate;
double periodHours;
/**
* #param employeeID
* #param firstName
* #param lastName
* #param listOfPaychecks
* Parameterized Constructor
*/
public Employee (int employeeID, String firstName, String lastName,
ArrayList<Paycheck> listOfPaychecks){
this.employeeID = employeeID;
this.firstName = firstName;
this.lastName = lastName;
this.listOfPaychecks = listOfPaychecks;
}
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID){
this.employeeID = employeeID;
}
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 ArrayList<Paycheck> getListOfPaychecks(){
return listOfPaychecks;
}
public void setListOfPaychecks(ArrayList<Paycheck> listOfPaychecks){
this.listOfPaychecks = listOfPaychecks;
}
#Override
public String toString() {
StringBuffer employeeInfo=new StringBuffer();
employeeInfo.append("Employee ID: "+employeeID+" First Name: "+firstName+" Last Name: "+lastName+"\nPay Check Details: \n");
for (Paycheck paycheck : listOfPaychecks) {
employeeInfo.append(paycheck.toString());
}
return employeeInfo.toString();
}
}
The problem is with your constructor when you call super - super has no such constructor
public HourlyEmployee(HourlyEmployee employee_1) {
super(employee_1);
this.hourlyRate = employee_1.hourlyRate;
this.periodHours = employee_1.periodHours;
}
Change your constructor like this:
public HourlyEmployee(HourlyEmployee employee_1) {
super(employee_1.employeeID, employee_1.firstName, employee_1.lastName, employee_1.listOfPaychecks);
this.hourlyRate = employee_1.hourlyRate;
this.periodHours = employee_1.periodHours;
}
Or better still like this:
public HourlyEmployee(HourlyEmployee employee_1) {
this (employee_1.employeeID, employee_1.firstName, employee_1.lastName, employee_1.listOfPaychecks, employee_1.hourlyRate, employee_1.periodHours);
}
I am trying to create an array of students which will contain 3 different types of students and each of the students will have 3 variables name, and 2 grades.
This is what I have done so far, and it gives me the following error cannot find symbol.
Main class:
public class JavaLab5 {
public static final int DEBUG = 0;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Student s[] = new Student[10];
s[0] = new MathStudent(Smith,14,15);
s[1] = new MathStudent(Jack,16,19);
s[2] = new MathStudent(Victor,18,21);
s[3] = new MathStudent(Mike,23,28);
s[4] = new ScienceStudent(Dave,32,25);
s[5] = new ScienceStudent(Oscar,28,56);
s[6] = new ScienceStudent(Peter,29,28);
s[7] = new ComputerStudent(Philip,25,38);
s[8] = new ComputerStudent(Shaun,34,39);
s[9] = new ComputerStudent(Scott,45,56);
for (int loop = 0; loop < 10; loop++) {
System.out.println(s[loop].getSubjects());
System.out.print(loop + " >>" + s[loop]);
}
}
}
This is the Student class:
public class Student {
private String name;
//private int age;
//public String gender = "na";
public static int instances = 0;
// Getters
//public int getAge() {
//return this.age;
//}
public String getName() {
return this.name;
}
// Setters
//public void setAge(int age) {
//this.age = age;
//}
public void setName(String name) {
if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
this.name = name;
}
/**
* Default constructor. Populates name,age and gender
* with defaults
*/
public Student() {
instances++;
//this.age = 18;
this.name = "Not Set";
//this.gender = "Not Set";
}
/**
* Constructor with parameters
* #param age integer
* #param name String with the name
*/
public Student(String name) {
//this.age = age;
this.name = name;
}
/**
* Gender constructor
* #param gender
*/
//public Student(String gender) {
//this(); // Must be the first line!
//this.gender = gender;
//}
/**
* Destructor
* #throws Throwable
*/
protected void finalize() throws Throwable {
//do finalization here
instances--;
super.finalize(); //not necessary if extending Object.
}
public String toString () {
return "Name: " + this.name; //+ " Age: " + this.age + " Gender: "
//+ this.gender;
}
public String getSubjects() {
return this.getSubjects();
}
}
and this is the MathStudent class which inherits from the Student class:
public class MathStudent extends Student {
private float algebraGrade;
private float calculusGrade;
/**
* Default constructor
* #param name
* #param algebraGrade
* #param calculusGrade
*/
public MathStudent(String name, float algebraGrade, float calculusGrade) {
super();
this.algebraGrade = algebraGrade;
this.calculusGrade = calculusGrade;
}
public MathStudent() {
super();
algebraGrade = 6;
calculusGrade = 4;
}
// Getters
public void setAlgebraGrade(float algebraGrade){
this.algebraGrade = algebraGrade;
}
public void setCalculusGrade(float calculusGrade){
this.calculusGrade = calculusGrade;
}
// Setters
public float getAlgebraGrade() {
return this.algebraGrade;
}
public float getCalculusGrade() {
return this.calculusGrade;
}
/**
* Display information about the subject
* #return
*/
#Override
public String getSubjects(){
return(" Math Student >> " + "Algebra Grade: " + algebraGrade
+ " Calculus Grade: " + calculusGrade);
}
}
Check how you instantiate students, i.e.
new MathStudent(Smith,14,15);
The name should be in quotes like "Smith"
new MathStudent("Smith",14,15);
Otherwise Smith will be interpreted as variable and this one is not defined.
I am new to using Java - so please forgive my ignorance. Would you be able to look at this code and let me know why I get error:
cannot find symbol - variable
when I call the constructor method. I am using BlueJ. Basically I put in the variables and then hit ok to create an object but it comes up with that error.
/**
* Write a description of class Membership here.
*
* #author (Gohar Warraich)
* #version (1.0)
*/
public class Membership
{
// instance variables - replace the example below with your own
private String firstname;
private String lastname;
private String phonenumber;
private int idnumber;
/**
* Constructor for objects of class Membership
*/
public Membership(String newfirstname, String newlastname, String newphonenumber, int newidnumber)
{
// initialise instance variables
firstname = newfirstname;
lastname = newlastname;
phonenumber = newphonenumber;
idnumber = newidnumber;
}
/**
* Accessor method of Membership
*/
public String getfirstname()
{
return firstname;
}
public String getlastname()
{
return lastname;
}
public String getphonenumber()
{
return phonenumber;
}
public int getidnumber()
{
return idnumber;
}
/**
* Mutator method of Membership
*/
public void setfirstname(String insertfirstname)
{
firstname = insertfirstname;
}
public void setlastname(String insertlastname)
{
lastname = insertlastname;
}
public void setphonenumber(String insertphonenumber)
{
phonenumber = insertphonenumber;
}
public void setid(int insertidnumber)
{
idnumber = insertidnumber;
}
public void printMembership()
{
System.out.println("The firstname is " + firstname + " The lastname is " + lastname +" The phoneNumber is "+ phonenumber +" The idNumber is " +idnumber);
}
}
#gohar, There isn't a problem in this code. It must be in your call to the constructor. I'll give you an example of what this should look like.
Membership membershipName = new Membership ("String", "String", "String", 0101)
0101 can be any int, and you can name the variable what ever you want by changing membershipName. Hope this helps. :)