/* 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 javaapplication3;
class Employee {
private String name;
private String address;
private int number;
public Employee(Employee emp) {
System.out.println("Constructing an Employee");
this.name = emp.name;
this.address = emp.address;
this.number = emp.number;
}
/*public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}*/
public void mailCheck() {
System.out.println("Mailing a check to " + this.name
+ " " + this.address);
}
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
class Salary extends Employee {
private double salary; //Annual salary
public Salary(Salary obj) {
super(obj);
setSalary(obj.salary);
}
/*public Salary(String name, String address, int number, double Salary) {
super(name, address, number);
setSalary(Salary);
}*/
public void mailCheck() {
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if (newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " + getName());
return salary / 52;
}
}
public class JavaApplication3 {
public static void main(String[] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta,UP", 3, 2000.00); // error why
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
}
}
If we removed the comments from the constructor, then it will be okay. Why?
Salary s = new Salary("Mohd Mohtashim", "Ambehta,UP", 3, 2000.00); you're calling a constructor that takes multiple parameters but it doesn't exist since you've commented it out .
Related
I am getting a syntax error in my main class when I call the constructor from another class that I need for the main program to run. This program is focused on inheritance and the appropriate calling of constructors and arguments. This is the error message I get during compilation:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "public", record expected after this token
at a6main.main(a6main.java:7)
This is the line of code that is causing the error:
PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821",
"2000", true, "1000");
The rest of the code can be found below:
class person {
String Name;
String Address;
String Telephone;
person (String Name, String Address, String Telephone) {
this.Name = Name;
this.Address = Address;
this.Telephone = Telephone;
}
String getName() {
return Name;
}
String getAddress() {
return Address;
}
String getTelephone() {
return Telephone;
}
void setName(String Name) {
this.Name = Name;
}
void setAddress(String Address) {
this.Address = Address;
}
void setTelephone(String Telephone) {
this.Telephone = Telephone;
}
}
public class customer extends person {
String number;
boolean OnMailingList;
//constructor and getters and setters
customer (String Name, String Address, String Telephone, String number, boolean OnMailingList) {
//inherit persons information
super(Name, Address, Telephone);
this.number = number;
this.OnMailingList = OnMailingList;
}
String getnumber() {
return number;
}
void setnumber(String number) {
this.number = number;
}
boolean OnMailingList () {
return OnMailingList;
}
void setOnMailingList(boolean OnMailingList) {
this.OnMailingList = OnMailingList;
}
}
public class PreferredCustomer extends customer {
private int purchase;
double discount;
/**public constructor so its accessible to main
* else ifs for certain percentage of discounts
* getters and setters for purchase and discount
* super to inherit other features from other classes */
public int getpurchase() {
return purchase;
}
public double getdiscount () {
return this.discount;
}
public void setPurchase(int purchase) {
this.purchase = purchase;
}
public PreferredCustomer(String Name, String Address, String Telephone, String number, int pur,
boolean OnMailingList, double Discount, PreferredCustomer preferredCustomer) {
super(Name, Address, Telephone, number, OnMailingList);
this.purchase = pur;
preferredCustomer.discount = discount;
if (this.purchase>= 2000) {
this.discount = 10;
} else if (this.purchase>= 1500) {
this.discount = 7;
} else if (this.purchase>= 1000) {
this.discount = 6;
} else if (this.purchase >= 500) {
this.discount = 5;
}
}
}
public class a6main {
public static void main (String [] args) {
public PreferredCustomer() {
}
PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821","2000", true, "1000");
System.out.println("Name: " + c.getName());
System.out.println("Address: " + c.getAddress());
System.out.println("Telephone number: " + c.getTelephone());
System.out.println("Customer ID: " + c.getnumber());
System.out.println("Amount spent: " + c.getpurchase());
System.out.println("On mailing list: " + c.OnMailingList());
System.out.println("Discount: " + c.getdiscount());
}
}
You have several mistakes here. I've corrected them, and the program launches, providing the result:
Name: Al
Address: 222BurdSt
Telephone number: 2102223321
Customer ID: 46821
Amount spent: 2000
On mailing list: true
Discount: 10.0
Remove PreferredCustomer constructor from the main method. It can't be a part of a
method, it is a part of a class. Then, the constructor for PreferredCustomer is already present in PreferredCustomer class.
Hopefully, your customer and PreferredCustomer classes are in separate files? If not, put them in separate files named customer.java and PreferredCustomer.java. In PreferredCustomer class constructor, remove PreferredCustomer preferredCustomer from arguments. It's redundant: why you need to pass one customer into another? Do customers have any relationships with each other? Now the number of arguments will match when you call the constructor (and don't use strings "2000", "1000" where should be integers):
PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821",
2000, true, 1000);
Further in the PreferredCustomer constructor, use this instead of preferredCustomer here: this.discount = Discount; and print Discount with upper case, as in the signature of the constructor.
As a result, the code of the constructor should be:
public PreferredCustomer(String Name, String Address, String Telephone, String number, int pur, boolean OnMailingList, double Discount) {
super(Name, Address, Telephone, number, OnMailingList);
this.purchase = pur;
this.discount = Discount;
if (this.purchase>= 2000) {
this.discount = 10;
} else if (this.purchase>= 1500) {
this.discount = 7;
} else if (this.purchase>= 1000) {
this.discount = 6;
} else if (this.purchase >= 500) {
this.discount = 5;
}
}
The main method in a6main class:
public static void main (String [] args) {
PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821", 2000, true, 1000);
System.out.println("Name: " + c.getName());
System.out.println("Address: " + c.getAddress());
System.out.println("Telephone number: " + c.getTelephone());
System.out.println("Customer ID: " + c.getnumber());
System.out.println("Amount spent: " + c.getpurchase());
System.out.println("On mailing list: " + c.OnMailingList());
System.out.println("Discount: " + c.getdiscount());
}
And take care of naming conventions, as other people pointed.
My programming assignment tasked me with writing an increase/decreasePay abstract method that must be put in my abstract employee class. I can't seem to get the the method correct in HourlyWorker so that it will take increase or decrease the pay by a "percentage". My math is sound (monthly pay - or + (monthly pay * the percentage), but my output in my test class is coming out the same after increasing/decreasing pay. Any help?
Employee class:
abstract public class Employee
{
private String lastName;
private String firstName;
private String ID;
public abstract void increasePay(double percentage);
public abstract void decreasePay(double percentage);
public abstract double getMonthlyPay();
public Employee(String last, String first, String ID)
{
lastName = last;
firstName = first;
this.ID = ID;
}
public void setLast(String last)
{
lastName = last;
}
public void setFirst(String first)
{
firstName = first;
}
public void setIdNumber(String ID)
{
this.ID = ID;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public String getName()
{
return firstName + lastName;
}
public String getIdNumber()
{
return ID;
}
}
HourlyWorkerClass
public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
public HourlyWorker(String last, String first, String ID, double rate)
{
super(last, first, ID);
hourlyRate = rate;
}
public void setHours(int hours)
{
this.hours = hours;
}
public int getHours()
{
return hours;
}
public void setHourlyRate(double rate)
{
if ( hours > 160 )
this.hourlyRate = hourlyRate * 1.5;
else
this.hourlyRate = rate;
}
public double getHourlyRate()
{
return hourlyRate;
}
public void setMonthlyPay(double monthlyPay)
{
monthlyPay = hourlyRate * hours;
}
public double getMonthlyPay()
{
return hourlyRate * hours;
}
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public void decreasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public String toString()
{
String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
+ getIdNumber() + " \nHourly Rate: " + hourlyRate;
return result;
}
}
Testing class (currently testing increase
public class TestEmployee2
{
public static void main(String[] args)
{
Employee [] staff = new Employee[3];
Supervisor sup = new Supervisor("Boss", "Jim", "JB7865", 54000);
HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 11.95);
hw1.setHours(200);
staff[0] = sup;
staff[1] = hw1;
System.out.println(staff[0].getMonthlyPay());
staff[0].increasePay(5);
System.out.println(staff[0].getMonthlyPay());
System.out.println(staff[1].getMonthlyPay());
staff[1].increasePay(10);
System.out.println(staff[1].getMonthlyPay());
}
}
Supervisor class:
public class Supervisor extends Employee
{
private double annualSalary;
private double monthlyPay;
public Supervisor(String last, String first, String ID, double salary)
{
super(last, first, ID);
annualSalary = salary;
}
public void setAnnualSalary(double salary)
{
annualSalary = salary;
}
public double getAnnualSalary()
{
return annualSalary;
}
public double getMonthlyPay()
{
return ((annualSalary + (annualSalary * .02)) / 12);
}
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public void decreasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public String toString()
{
String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
+ getIdNumber() + "\nAnnual Salary: " + annualSalary;
return result;
}
}
Output is:
4590.0
4590.0
2390.0
2390.0
Doesn't appear to be modifying getMonthlyPay()
Should be:
4590.00
4819.50
2390.00
2629.00
In increasePay, you are increasing monthlyPay:
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
But when you getMonthlyPay, you calculate the pay using two other variables - hourlyRate and hours:
public double getMonthlyPay()
{
return hourlyRate * hours;
}
So changing monthlyPay doesn't affect what getMonthlyPay returns. I suspect a similar thing happens in Supervisor.
increasePay should instead increase hourlyRate:
public void increasePay(double percentage)
{
hourlyRate *= 1 + percentage / 100;
}
Also, I don't think you need a monthlyPay field (or setMonthlyPay, for that matter) in HourlyEmployee at all. The monthly rate can always be calculated by hours and hourlyRate.
For Supervisor, do the same thing, and change annualSalary rather than monthlyPay:
public double getMonthlyPay()
{
return ((annualSalary + (annualSalary * .02)) / 12);
}
public void increasePay(double percentage)
{
annualSalary *= 1 + percentage / 100;
}
Im trying to create a student class, a course class and the main class. I am trying to add students to the course, and when students are added to the class the number of students in the course should increase, when the the code is run it should print the course details followed by the students in the course.
I have got the following code:
Main class:
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
//Student student = new Student(); // Calling default constructor here.
Course course = new Course();
student = new Student(21, "Joe", "CSE", "07447832342");
course = new Course("CSE", "Tom", 5);
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course);
System.out.println();
System.out.println("Student contains: "); // calls student.toString());
System.out.println("-------------------");
System.out.println(student);
}
}
Course class:
public class Course {
ArrayList<Student> studentList;
private String courseName;
private String teacher;
private int noOfStudents;
//Getters
public String getCourseName(){
return this.courseName;
}
public int getNoOfStudents(){
return this.noOfStudents;
}
public String getTeacher(){
return this.teacher;
}
//Setters
public void setCourseName(String courseName){
this.courseName = courseName;
}
public void setNoOfStudents(int noOfStudents){
this.noOfStudents = noOfStudents;
}
public void setTeacher(String teacher){
this.teacher = teacher;
}
/**
* Default constructor. Populates course name, number of students with defaults
*
*/
public Course(){
this.noOfStudents = 0;
this.courseName = "Not Set";
this.teacher = "Not Set";
studentList = new ArrayList<Student>();
}
/**
* Constructor with parameters
* #param noOfStudents integer
* #param courseName String with the Course name
* #param teacher String with the teacher
*/
public Course(String courseName, String teacher, int noOfStudents){
this.courseName = courseName;
this.teacher = teacher;
noOfStudents = noOfStudents;
studentList = new ArrayList<Student>();
}
public static void addStudent(Student newStudent){
if(studentList.size()==noOfStudents){
System.out.println("The class is full, you cannot enrol.");
}
else {
studentList.add(newStudent);
}
}
public String toString() {
return "Course Name: " + this.courseName + " Teacher: " + this.teacher
+ " Number of Students: " + this.noOfStudents;
}
}
Student class:
public class Student {
private String name;
private int age;
public String gender = "na";
private String course;
private String phoneNo;
public static int instances = 0;
// Getters
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public String getCourse(){
return this.course;
}
public String getPhoneNo(){
return this.phoneNo;
}
// Setters
public void setAge(int age){
this.age = age;
}
public void setName(String name){
if (JavaLecture3.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
this.name = name;
}
public void setCourse(String course){
this.course = course;
}
public void setPhoneNo(String phoneNo){
this.phoneNo = phoneNo;
}
/**
* Default constructor. Populates name,age,gender,course and phone Number
* with defaults
*/
public Student(){
instances++;
this.age = 18;
this.name = "Not Set";
this.gender = "Not Set";
this.course = "Not Set";
this.phoneNo = "Not Set";
}
/**
* Constructor with parameters
* #param age integer
* #param name String with the name
* #param course String with course name
* #param phoneNo String with phone number
*/
public Student(int age, String name, String course, String phoneNo){
this.age = age;
this.name = name;
this.course = course;
this.phoneNo = phoneNo;
}
/**
* Gender constructor
* #param gender
*/
public Student(String gender){
this(); // Must be the first line!
this.gender = gender;
}
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 + " Course: " + this.course + " Phone number: "
+ this.phoneNo;
}
}
Reference:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html?is-external=true
Course.getNoOfStudents should just return studentList.size(). No need to maintain a separate variable.
To print a list of students (from Course):
for(int i=0;i<studentList.size();i++) System.out.println(studentList.get(i));
Since you initialized the array list in the Course class, you should add the students to it. You should create a method in Course that adds an object to the array list such as:
public void addStudent(Student s){
studentList.add(s);
noOfStudents++;
}
To add multiple students:
public void addStudents(Student[] students){
for(int i = 0; i < students.length; i++){
studentList.add(students[i]);
}
}
You're almost there, just use the .add method made for you in the array list.
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
Student student = new Student(); // Calling default constructor here.
Course course = new Course();
student = new Student(21, "Joe", "CSE", "07447832342");
course = new Course("CSE", "Tom", 5);
course.addStudent(student);
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course);
System.out.println();
System.out.println("Student contains: "); // calls student.toString());
System.out.println("-------------------");
System.out.println(student);
}
}
public class Course {
List<Student> studentList;
private String courseName;
private String teacher;
private int noOfStudents;
// Getters
public String getCourseName() {
return this.courseName;
}
public int getNoOfStudents() {
return this.noOfStudents;
}
public String getTeacher() {
return this.teacher;
}
// Setters
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public void setNoOfStudents(int noOfStudents) {
this.noOfStudents = noOfStudents;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
/**
* Default constructor. Populates course name, number of students with
* defaults
*
*/
public Course() {
this.noOfStudents = 0;
this.courseName = "Not Set";
this.teacher = "Not Set";
studentList = new ArrayList<Student>();
}
/**
* Constructor with parameters
*
* #param noOfStudents
* integer
* #param courseName
* String with the Course name
* #param teacher
* String with the teacher
*/
public Course(String courseName, String teacher, int noOfStudents) {
this.courseName = courseName;
this.teacher = teacher;
this.noOfStudents = noOfStudents;
studentList = new ArrayList<Student>();
}
public void addStudent(Student newStudent) {
if (studentList.size() == noOfStudents) {
System.out.println("The class is full, you cannot enrol.");
} else {
studentList.add(newStudent);
}
}
#Override
public String toString() {
return "Course Name: " + this.courseName + " Teacher: " + this.teacher
+ " Number of Students: " + studentList.size();
}
}
public class Student {
private String name;
private int age;
public String gender = "na";
private String course;
private String phoneNo;
public static int instances = 0;
// Getters
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public String getCourse() {
return this.course;
}
public String getPhoneNo() {
return this.phoneNo;
}
// Setters
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
if (JavaLecture3.DEBUG > 3)
System.out.println("In Student.setName. Name = " + name);
this.name = name;
}
public void setCourse(String course) {
this.course = course;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
/**
* Default constructor. Populates name,age,gender,course and phone Number
* with defaults
*/
public Student() {
instances++;
this.age = 18;
this.name = "Not Set";
this.gender = "Not Set";
this.course = "Not Set";
this.phoneNo = "Not Set";
}
/**
* Constructor with parameters
*
* #param age
* integer
* #param name
* String with the name
* #param course
* String with course name
* #param phoneNo
* String with phone number
*/
public Student(int age, String name, String course, String phoneNo) {
this.age = age;
this.name = name;
this.course = course;
this.phoneNo = phoneNo;
}
/**
* Gender constructor
*
* #param gender
*/
public Student(String gender) {
this(); // Must be the first line!
this.gender = gender;
}
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 + " Course: " + this.course + " Phone number: "
+ this.phoneNo;
}
}
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
//Create course object
Course course = new Course("CSE", "Tom", 5);
Scanner scanner = new Scanner(System.in);
String cmd = "Yes";
while(cmd.equals("Yes")){
Student student = new Student();
System.out.print("Enter a new student? ");
cmd = scanner.next();
if (cmd.equals("Yes")){
//Read student name
System.out.print("Enter a student name: ");
String name = scanner.next();
student.setName(name);
//Read student Age
System.out.print("Enter a student age: ");
int age = scanner.nextInt();
student.setAge(age);
//Read student Course
System.out.print("Enter a student course: ");
String stdent_course = scanner.next();
student.setCourse(stdent_course);
//register the student to the class
course.addStudent(student);
}
}
scanner.close();
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course.toString());
System.out.println();
}
}
I reworked a bit your toString() method from Course class. This should print the list of students attending a class.
public String toString (){
String ret_value = "Name: " + this.name + " Age: " + this.age + " Gender: "
+ this.gender + " Course: " + this.course + " Phone number: "
+ this.phoneNo + " Students attending this course:";
for (Student student: studentList) {
ret_value = ret_value + " " + Student.getName();
}
return ret_value;
}
constructor Student in class javaapplication 14.Student cannot be
applied to given types; required:
java.lang.String,int,java.lang.String,java.lang.String,double found:
java.lang.String,double reason: actual and formal argument lists
differ in length
This is the line that gives me error
super(idNum, gpa);
here is my code:
package javaapplication14;
public class JavaApplication14
{
public static void main(String[] args)
{
Person bob = new Person("Coach Bob", 27, "M");
System.out.println(bob);
Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
System.out.println(lynne);
Teacher mrJava = new Teacher("Duke Java",34,"M","Computer Science",50000);
System.out.println(mrJava);
}
}
class Person
{
protected String myName ;
protected int myAge;
protected String myGender;
public Person(String name, int age, String gender)
{
myName = name;
myAge = age ;
myGender = gender;
}
#Override
public String toString()
{
return myName + ", age: " + myAge + ", gender: " +myGender;
}
//These are the set methods
void setName(String name)
{
myName = name;
}
void setAge(int age)
{
myAge = age ;
}
void setGender(String gender)
{
myGender = gender;
}
//These are the get methods
String myName()
{
return myName;
}
int myAge()
{
return myAge;
}
String myGender()
{
return myGender;
}
}
class Student extends Person
{
protected String myIdNum;
protected double myGPA;
public Student(String name, int age, String gender,String idNum, double gpa)
{
super(name, age, gender);
myIdNum = idNum;
myGPA = gpa;
}
//These are the set methods
void setmyIdNum(String idNum)
{
myIdNum = idNum;
}
void setmyGPA(double gpa)
{
myGPA = gpa;
}
#Override
public String toString()
{
return super.toString() + ", student id: " + myIdNum + ", gpa: " + myGPA;
}
public String myIdNum()
{
return myIdNum;
}
double myGPA()
{
return myGPA;
}
}
class Teacher extends Person
{
public double mySalary;
public String mysubjectName;
public Teacher( String name, int age, String gender, String
subjectName,double salary )
{
super(name, age, gender);
mysubjectName = subjectName;
mySalary = salary;
}
//These are the setter methods
void setsubjectname(String subjectName)
{
mysubjectName = subjectName;
}
void setsalary(double Salary)
{
mySalary = Salary;
}
#Override
public String toString()
{
return super.toString() + ", subject name: " + mysubjectName + ", Salary: "
+ mySalary;
}
//These are the getter methods
public String getsubjectname(String subjectName)
{
return subjectName;
}
double getsalary(double Salary)
{
return Salary ;
}
class CollegeStudent extends Student
{
public String myMajor;
public int myYear;
public CollegeStudent(String name, int age,String gender,
String idNum, double gpa, int year, String major )
{
super(idNum, gpa);
myMajor = major;
myYear = year;
}
void setmajor(String Major)
{
myMajor = Major;
}
void setyear(int Year)
{
myYear = Year;
}
#Override
public String toString()
{
return super.toString() + ", major: " + myMajor + ", Year: " + myYear;
}
//These are the getter methods
public String getmajor(String myMajor)
{
return myMajor;
}
public int getyear(int myYear)
{
return myYear ;
}
}
}
There is no such constructor with two parameters in Student class
The class Student and also the class Person has no constuctor with the signature (String, double).
So you have to create this constructor in one of the superclasses or you have to call an other constructor.
Your super class Student has following constructor with 5 parameter
public Student(String name, int age, String gender,String idNum, double gpa)
But, at CollegeStudent constructor, super(idNum, gpa); expect a constructor with two parameter Student(String idNum, double gpa), which is missing in superclass. So, define a constructor at Student class like:
class Student extends Person {
public Student(String idNum, double gpa){
}
}
I am trying to verify what is wrong with my class builds to make address class communicate with my warehouse class. My get() and set methods along with toString() is in question. Any help would be greatly appreciated
public class Address
{
private String street;
private String city;
private String state;
private int zip;
public Address()
{
setStreet("");
setCity("");
setState("");
setZip(0);
}
public Address(String str, String c, String sta, int z)
{
setStreet(str);
setCity(c);
setState(sta);
setZip(z);
}
public String getStreet()
(
return street;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public int getZip()
{
return zip;
}
public void setStreet( String str)
{
street = str;
}
public void setCity( String c)
{
city = c;
}
public void setState( String sta )
{
state = sta;
}
public void setZip( String z )
{
zip = z;
}
public String toString()
{
return( " Street " + street + " City " + city +" State " + state + " Zip " + zip);
}
}
Must communicate with the following warehouse class:
public class Warehouse
{
private double squareFeetSize;
private double pricePerSquareFoot;
private int televisions;
private int computers;
private Address address;
public Warehouse()
{
setSquareFeetSize(0.0);
setPricePerSquareFoot(0.0);
setTelevisions(0);
setComputers(0);
setAddress(new Address());
}
public Warehouse( double s, double p, int t, int c, Address a)
{
setSquareFeetSize(s);
setPricePerSquareFoot(p);
setTelevisions(t);
setComputers(c);
setAddress(a);
}
public double getSquareFeetSize()
{
return squareFeetSize;
}
public double getPricePerSquareFoot()
{
return pricePerSquareFoot;
}
public int getTelevisions()
{
return televisions;
}
public int getComputers()
{
return computers;
}
public Address getAddress()
{
return address;
}
public void setSquareFeetSize( double s)
{
squareFeetSize = s;
}
public void setPricePerSquareFoot ( double p )
{
pricePerSquareFoot = p;
}
public void setTelevisions ( int t )
{
televisions = t;
}
public void setComputers ( int c)
{
computers = c;
}
public void setAddress( Address a)
{
address = a;
}
public String toString()
{
return (" Square Foot Size " + squareFeetSize + " Price Per Square Foot " +
pricePerSquareFoot + " Televisions " + televisions + " Computers " +
computers + " Address " + address.toString());
}
public double calculateWarehouseCharge()
{
double charge = 0.0;
charge = (squareFeetSize * pricePerSquareFoot + (2.25 * televisions) + (5.50 * computers));
return charge;
}
public double purchaseTelevision( int quantity, double price)
{
double cost = 0.0;
if( quantity > televisions)
{
JOptionPane.showMessageDialog(null, "Television Quantity Unavailable");
price = 0.0;
}
else
{
cost = price * quantity;
televisions = televisions - quantity;
}
return cost;
}
public double purchaseComputer( int quantity, double price)
{
double cost = 0.0;
if( quantity > computers)
{
JOptionPane.showMessageDialog(null, "Computer Quantity Unavailable");
price = 0.0;
}
else
{
cost = price * quantity;
computers = computers - quantity;
}
return cost;
}
}
class Address{
private street = "123 Lame Street";
public getStreet(){
return street;
}
}
class Warehouse{
Address address = new Address(); // create an instance; not just declare
public void shipping(){
shipTo(address.getStreet()); // Here's how to "communicate"
}
}
You are on correct way. Your code is working. Because ToString is method of Object class that you overwrite in your class. http://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/api/java.lang.Object.html
Are you not getting correct result ?