How do I acces the methods within the extended class? - java

I have the following:
main.java
public class main {
public static void main(String[] args){
Player startingPitcher = new Player("Doug", "Mellon",
"Pitcher", 29);
startingPitcher.setThrowingArm("right");
}
}
Player.java
class Player
{
private String firstName, lastName, position;
private int age;
public Player(String firstName, String lastName, String position, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.position = position;
this.age = age;
}
private String getFirstName(){
return this.firstName;
}
private void setFirstName(String newFirstName){
this.firstName = newFirstName;
}
private String getLastName(){
return this.lastName;
}
private void setLastName(String newLastName){
this.lastName = newLastName;
}
private String getPosition(){
return this.position;
}
private void setPosition(String newPosition){
this.position = newPosition;
}
private int getAge(){
return this.age;
}
private void setAge(int newAge){
this.age = newAge;
}
}
Pitcher.java
public class Pitcher extends Player{
public String throwingArm;
public int fastballMPH;
public Pitcher(String firstName, String lastName, String position, int age,
String throwingArm, int fastballMPH) {
super(firstName, lastName, position, age);
this.throwingArm = throwingArm;
this.fastballMPH = fastballMPH;
}
public String getThrowingArm(){
return this.throwingArm;
}
public void setThrowingArm(String newThrowingArm){
this.throwingArm = newThrowingArm;
}
private int getFastballMPH(){
return this.fastballMPH;
}
private void setFastballMPH(int newFastballMPH){
this.fastballMPH = newFastballMPH;
}
}
My main is throwing the following error:
Error:(6, 24) java: cannot find symbol symbol: method
setThrowingArm(java.lang.String) location: variable startingPitcher
of type Player
I understand the error - I think - but I thought you could access the methods if you were using inheritance.
How can I set the throwing arm for the Player object in my main?
Sorry if this question is worded poorly. If there is anything I can add to clarify my question, please don't hesitate to ask.
Thank you all very much for your time.

Variable startingPitcher in method main(), of class main, is an instance of Player and not an instance of Pitcher, hence it has no setThrowingArm() method.
You need to create an instance of Pitcher, i.e.
Pitcher startingPitcher = new Pitcher("Doug", "Mellon", "pitcher", 29, "right", 90);
Note that this is what the error message is telling you, namely that variable startingPitcher is an instance of Player (and not an instance of Pitcher).

Related

Calling a private method within another class into a class

I'm trying to call method within the BasicInfo class (full name(), alsoKnownAs()) into another class called Customer, specifically within the displayInfo() portion, but am not sure how to do that, here is my code:
enum Gender {MALE, FEMALE}
class BasicInfo
{
private String firstName, secondName, lastName;
private Gender g;
//Default Constructor
public BasicInfo()
{
//Do nothing
}
//Other Constructor
public BasicInfo(String firstName, String secondName, String lastName, Gender g)
{
this.firstName = firstName;
this.secondName = secondName;
this.lastName = lastName;
this.g = g;
}
//Copy Constructor
public BasicInfo(BasicInfo bi)
{
this.firstName = bi.firstName;
this.secondName = bi.secondName;
this.lastName = bi.lastName;
this.g = bi.g;
}
public String getFirstName()
{
return firstName;
}
public String getSecondName()
{
return secondName;
}
public String getLastName()
{
return lastName;
}
private Gender getGender()
{
return g;
}
public void setInfo(String firstName, String secondName, String lastName, Gender g)
{
this.firstName = firstName;
this.secondName = secondName;
this.lastName = lastName;
this.g = g;
}
private String fullName()
{
return (firstName + " " + secondName + " " + lastName);
}
private String alsoKnownAs()
{
return (firstName.charAt(0) + ". " + secondName.charAt(0) + ". " + lastName);
}
public void displayInfo()
{
System.out.printf("Full name: %s%n", fullName());
System.out.printf("Also known as: %s%n", alsoKnownAs());
System.out.printf("Gender: %s%n", getGender());
}
}
class Customer
{
private BasicInfo bi;
private int birthYear;
public Customer()
{
//Do nothing
}
public Customer(BasicInfo bi, int birthYear)
{
this.bi = bi;
this.birthYear = birthYear;
}
public Customer(Customer c)
{
this.bi = c.bi;
this.birthYear = c.birthYear;
}
public BasicInfo getBasicInfo()
{
return bi;
}
public int getBirthYear()
{
return birthYear;
}
public void setInfo(BasicInfo bi, int birthYear)
{
this.bi = bi;
this.birthYear = birthYear;
}
public void displayInfo()
{
System.out.printf("Full name: %s%n", bi.fullName());
System.out.printf("Also known as: %s%n", bi.alsoKnownAs());
System.out.printf("Gender: %s%n", bi.getGender());
System.out.printf("Year of birth: %d%n", birthYear);
}
}
Within customer class, displayInfo(), I used "bi.fullName()" and "bi.alsoKnownAs()" and "bi.getGender()", is this the right way to method call? Any help would be greatly appreciated :)
I tried using "BasicInfo.'the method'" as well, but still resulted in a compilation error.
This is against Java principles. If a method is designed to be called outside of its class then it cannot be private.
You should change access specifier of this methods to protected/public or default as the private method can be accessed only within the current class.
Thanks everyone for taking the time to read my post.
public void displayInfo()
{
System.out.printf("Full name: %s%n", bi.fullName());
System.out.printf("Also known as: %s%n", bi.alsoKnownAs());
System.out.printf("Gender: %s%n", bi.getGender());
System.out.printf("Year of birth: %d%n", birthYear);
}
I changed the above to this:
public void displayInfo()
{
bi.displayInfo();
System.out.printf("Year of birth: %d%n", birthYear);
}
Which displayed the information from the BasicInfo class's displayInfo() method. Perhaps my question wasn't very clear in the first place. Instead of asking how to access a private method, I should have asked how to call displayInfo() method from the BasicInfo class into the Customer class.

Print ArrayList(My Datatype) using For Each Loop Not Working

Here is my code of UserDetail class.
public class UserDetail {
public void user_detail() {
Members m1=new Members("Rashid Faheem","0312-6193172","House No. 430, Street No. 5 Mehmood Abad Pindora, Rawalpindi");
Members m2=new Members("Yawar Hayat","0312-6193172", "RajanPur");
Members m3=new Members("Azhar Malik", "0312-6193172", "RajanPur");
Members m4=new Members("Muhammad Ali", "0312-6193172", "RajanPur");
Members m5=new Members("Muhammad Nazik", "0312-6193172", "RajanPur");
ArrayList<Members> al = new ArrayList<Members>();
al.add(m1);
al.add(m2);
al.add(m3);
al.add(m4);
al.add(m5);
System.out.println("These are our Members.");
For (Members m:al) {
System.out.println(m.getName());
}
}
}
Here is code from Members Class which I am using as DataType in ArrayList.
public class Members {
private String name, phone,address;
public Members(String name, String phone, String address) {
name=name;
phone=phone;
address=address;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
}
Update
I changed my code according to your advice but another problem now. I am only getting name but not phone and address.
Output
These are our Members.
Rashid Faheem
Yawar Hayat
Azhar Malik
Muhammad Ali
Muhammad Nazik
for (Members m:al) {
System.out.println(m.getName());
}
Notice the lowercase f. Just a typo.
EDIT:
Your constructor is also just assigning the constructor arguments to themselves. You need to assign the constructor arguments to the class variables.
public Members(String name, String phone, String address) {
this.name = name;
this.phone = phone;
this.address = address;
}
You should replace For by for.
And update Members with additionnal this :
public Members(String name, String phone, String address)
{
this.name=name;
this.phone=phone;
this.address=address;
}
There are two syntax errors. Once you correct them you should be able to print the members list:
for not For.
for (Members m : al) {
System.out.println(m.getName());
}
Members constructor. The code is not assigning the parameters to the member variables.
public Members(String name, String phone, String address) {
this.name=name;
this.phone=phone;
this.address=address;
}
Refer https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
First of all, you should stick to the Java naming conventions - so your method user_detail should be named userDetail. You might want to consider naming it print as that's what it is supposed to do.
Next, the constructor in your Members class does nothing, since you miss the this. as prefix of the left side of your operation. So you would want to write it like this (as mentioned by WatchdogReset):
public Members(String name, String phone, String address) {
this.name = name;
this.phone = phone;
this.address = address;
}
Another problem is the typo in For which is supposed to be for (stated by ninnemank).
I refactored your code, to show you a possible way of doing what you want - of course this is rather opinionated and not a perfect solution.
Members
public class Members {
private String name;
private String phone;
private String address;
public Members(String name, String phone, String address) {
this.name = name;
this.phone = phone;
this.address = address;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
#Override
public String toString(){
return String.join(" / ", this.getName(), this.getPhone(), this.getAddress());
}
}
UserDetail
import java.util.ArrayList;
import java.util.List;
public class UserDetail {
private List<Members> members;
public UserDetail() {
this.members = new ArrayList<>();
}
public boolean addMember(Members member){
return this.members.add(member);
}
#Override
public String toString(){
return this.members.stream()
.map(member -> member.toString())
.reduce((a, b) -> String.join(System.lineSeparator(), a, b))
.orElse("");
}
}
Main
public class Main {
public static void main(String[] args) {
Members m1 = new Members("Rashid Faheem", "0312-6193172", "House No. 430, Street No. 5 Mehmood Abad Pindora, Rawalpindi");
Members m2 = new Members("Yawar Hayat", "0312-6193172", "RajanPur");
Members m3 = new Members("Azhar Malik", "0312-6193172", "RajanPur");
Members m4 = new Members("Muhammad Ali", "0312-6193172", "RajanPur");
Members m5 = new Members("Muhammad Nazik", "0312-6193172", "RajanPur");
UserDetail userDetail = new UserDetail();
userDetail.addMember(m1);
userDetail.addMember(m2);
userDetail.addMember(m3);
userDetail.addMember(m4);
userDetail.addMember(m5);
System.out.println("These are our Members.");
System.out.println(userDetail);
}
}
Output
These are our Members.
Rashid Faheem / 0312-6193172 / House No. 430, Street No. 5 Mehmood Abad Pindora, Rawalpindi
Yawar Hayat / 0312-6193172 / RajanPur
Azhar Malik / 0312-6193172 / RajanPur
Muhammad Ali / 0312-6193172 / RajanPur
Muhammad Nazik / 0312-6193172 / RajanPur

Polymorphism issue

I have been reading on how to program Java 8 Polymorphism.
I have this code:
public class Person
{
// instance variables - replace the example below with your own
private String lastname;
private String firstname;
private int age;
private boolean married;
private float salary;
/**
* Constructor for objects of class Person
*/
public Person(String lastname, String firstname, int age, boolean married, float salary)
{
// initialise instance variables
this.lastname = new String(lastname);
this.firstname = new String(firstname);
this.age = age;
this.married = married;
this.salary = salary;
}
public String getLastName() {return lastname;}
public String getFirstName() {return firstname;}
public int getAge() {return age;}
public boolean isMarried() {return married;}
public float getSalary() {return salary;}
}
public class MarriedPerson extends Person
{
// instance variables - replace the example below with your own
private int children;
/**
* Constructor for objects of class MarriedPerson
*/
public MarriedPerson(String lastname, String firstname, int age, float salary, int children)
{
// initialise instance variables
super(lastname, firstname, age, true, salary);
this.children = children;
}
public int getNoOfChildren()
{
// put your code here
return children;
}
}
abstract class MyTester
{
public static void main(String[] args) {
Person p1 = new Person("Kings", "Paul", 22, true, 1200f);
MarriedPerson mp1 = new MarriedPerson("Tront", "Betty", 31, 980.5f, 3);
System.out.print(p1.getFirstName()+" "+p1.getLastName()+" is "
+p1.getAge()+" years old, gets a "+p1.getSalary()
+" Euros salary and is");
if (p1.isMarried() == false)
System.out.print(" not");
System.out.println(" married.");
System.out.print(mp1.getFirstName()+" "+mp1.getLastName()
+" is " +mp1.getAge()+ " years old, gets a " + mp1.getSalary()
+" Euros salary and is" + " married with ");
if (mp1.getNoOfChildren() > 0)
System.out.print(mp1.getNoOfChildren());
else System.out.print("no");
System.out.println(" children.");
}
}
Reading from a book, I have not seen an abstract class that contains the main function so I am a bit confused.
Why have we declared MyTester class as abstract? Is this necessary?
I have now created a printInfo method in class Person. See following code:
public class Person
{
// instance variables - replace the example below with your own
private String lastname;
private String firstname;
private int age;
private boolean married;
private float salary;
/**
* Constructor for objects of class Person
*/
public Person(String lastname, String firstname, int age, boolean married, float salary)
{
// initialise instance variables
this.lastname = new String(lastname);
this.firstname = new String(firstname);
this.age = age;
this.married = married;
this.salary = salary;
}
public String getLastName() {return lastname;}
public String getFirstName() {return firstname;}
public int getAge() {return age;}
public boolean isMarried() {return married;}
public float getSalary() {return salary;}
public void printInfo(){
System.out.print(p1.getFirstName()+" "+p1.getLastName()+" is "
+p1.getAge()+" years old, gets a "+p1.getSalary()
+" Euros salary and is");
if (p1.isMarried() == false)
System.out.print(" not");
System.out.println(" married.");
}
}
abstract class MyTester
{
public static void main(String[] args) {
Person p1 = new Person("Kings", "Paul", 22, true, 1200f);
MarriedPerson mp1 = new MarriedPerson("Tront", "Betty", 31, 980.5f, 3);
p1.printInfo();
mp1.printInfo();
}
}
When I compile the modified code it give me the error: cannot find symbol - variable p1. Why is that? What do I need to do in order to fix that?
What advantages second code has over first one?
It does not make sense MyTester to be abstract since it does not have any abstract method. The only reason might be to prevent the instantiation of MyTester class because it might not make sense for an instance to exist.
MyTester is declared abstract so you can't create any instances of it (there is no reason you would want to). See this link, it says:
An abstract class is a class that is declared abstract—it may or may
not include abstract methods. Abstract classes cannot be instantiated,
but they can be subclassed.
When I compile the modified code it give me the error: cannot find symbol - variable p1. Why is that? What do I need to do in order to fix that?
For future reference: You should create a new question and not edit your existing one. Also, if someone solved your first problem you should allways be so nice as to accept their answer. ;)
Anyway: You are getting that error because your object is only "known" as p1 in your main class.
In your "printInfo()" method you are trying to have the objects do calls on itself. So either just remove the "p1." part and just call the methods like "getFirstName()", or explicitly call "this.getFirstName()".
("this" is a java keyword refering to the current object instance.)
A tipp for you: Get a good JAVA IDE like Eclipse (https://eclipse.org/) and you will see simple errors like those even before compiling.
No. It makes no difference, as the class is never extended or used except as an entrypoint.

How to debug a java code with getters and setters?

I was given an empty class and instructed to fill it with instance variables and methods with bodies, which set or get values from the instance variables. Then I am to open another java file where code creating a new Contact object was provided and instructed to add additional code that uses that object to test all the methods created in the Contact class.
I am using the program Eclipse for coding/testing. The only things are correct are my method headers. When testing with what I have written (below) I get a null, null response (that is when I remove what wrote about the setPhoneNumber. When it's included I get a worse looking error
Exception in thread "main" java.lang.Error: Unresolved compilation
problem: phoneNumber cannot be resolved to a variable.)
Any assistance on getting the get/set methods to work will be much appreciated. I am entirely new to any programming and have looked at about dozens of examples and have tried many variations with no success.
public class Contact
{
//Instance Variables
private String firstName;
private String lastName;
private int phoneNumber;
public String getFirstName() //method to retrieve first name
{
return this.firstName;
}
public void setFirstName(String firstName) //method to set the first name
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public int getPhoneNumber()
{
return this.phoneNumber;
}
public void setPhoneNumber(int phoneNumber)
{
this.phoneNumber = phoneNumber;
}
public void call()
{
System.out.printf(getFirstName(), getLastName(), getPhoneNumber());
}
}
Here is the other java file used to test the Contact class above:
public class ContactTestDriver
{
public static void main(String[] args)
{
Contact contact = new Contact();
//all above was given; below are my code additions
String myContact;
myContact = contact.getFirstName();
contact.setFirstName();
System.out.println(myContact);
myContact = contact.getLastName();
contact.setLastName();
System.out.println(myContact);
int myNumber;
myNumber = contact.getPhoneNumber();
contact.setPhoneNumber(phoneNumber);
System.out.println(myNumber);
}
}
After some helpful comments I have made changes but still no success. My error is "myContact cannot be resolved to a variable"
Here is the revised code:
public class Contact
{
//Instance Variables
private String firstName;
private String lastName;
private int phoneNumber;
public void setFirstName(String firstName) //method to set the first name
{
this.firstName = firstName;
}
public String getFirstName() //method to retrieve first name
{
return this.firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getLastName()
{
return lastName;
}
public void setPhoneNumber(int phoneNumber)
{
this.phoneNumber = phoneNumber;
}
public int getPhoneNumber()
{
return this.phoneNumber;
}
public void call()
{
System.out.printf("Who to call ", getFirstName(), getLastName(), getPhoneNumber());
}
}
And this is the code to test the Contact class:
public class ContactTestDriver
{
public static void main(String[] args)
{
Contact contact = new Contact();
//all above was given; below are my code additions
String firstName = "a"; //define first name
contact.setFirstName(firstName); //then set it
myContact = contact.getFirstName(); //then get it
System.out.println(myContact);
String lastName;
contact.setLastName(lastName);
myContact = contact.getLastName();
System.out.println(myContact);
int myNumber = 123;
contact.setPhoneNumber(phonNumber);
myNumber = contact.getPhoneNumer();
System.out.println(myNumber);
}
}
When you call contact.setFirstName(); which take String parameter you didn't pass the first name
public void setFirstName(String firstName){...}
and also with contact.setLastName(); : this also needs to send parameter
like this :
String lastName="//last name";
contact.setLastName(lastName);
and you need to know that we use the setter method before getter method
so first set the names then get it
String firstName = "a";////define the first name
contact.setFirstName(firstName); //// then set it
myContact = contact.getFirstName(); /// then get it
System.out.println(myContact);
String lastName = "b";
contact.setLastName(lastName);
myContact = contact.getLastName();
System.out.println(myContact);
int myNumber = 123;
contact.setPhoneNumber(myNumber);
myNumber = contact.getPhoneNumber();
System.out.println(myNumber);
This is what you have:
myContact = contact.getFirstName();
contact.setFirstName();
First you are getting, then you are setting. Reversing them makes more sense:
contact.setFirstName();
myContact = contact.getFirstName();
Moreover, set methods like setFirstName should receive a String parameter:
contact.setFirstName("Johny");
You can use Eclipse debugging tool.Run you java class in debug mode and add break points where ever you need to analyze data.
getter is just a method getting a field and setter is setting a new field.
you must set some values first i.e
int myNumber;
myNumber = contact.getPhoneNumber();
contact.setPhoneNumber(phoneNumber);
System.out.println(myNumber);
int myNumber= "983445556"; // here you are creating an instance
or you can do it by
contact.setPhoneNumber(32435435345);
similarly for rest cases
Here phoneNumber is not a defined variable in your main method
int myNumber;
myNumber = contact.getPhoneNumber();
contact.setPhoneNumber(phoneNumber);
it should be defined.

Homework Help; Reading from multiple files into Vectors/Arrays

Okay my assignment is to read in from 2 separate files. The first file reads in "HallOfFameMember" and should then be stored in a vector.
The second should read in and then be stored to an array of HallOfFame
The HallOfFameMember object has 10 fields. In order firstName, lastName, deceased, month born, day born, year born, totalEarned, yearsPlayed, yearInducted, hall of fame id
The day/month/year born are all going to a separate class to create a date born. The date born is then set within HallOfFameMember. The Fields for a record will be separated by varying numbers of spaces and/or tabs (whitespaces).
After reading a record from this file, call the 10-arg constructor of HallOfFameMember, using the fields from the record as arguments in the constructor call. Add this new HallOfFameMember object to tempHallOfFameMemberVec.
The HallOfFame object has 5 fields. In order hallOfFameId, city, costToVisit, numberOfVisitorsPerYear, and name.
After reading a record from this file, call the 5-arg constructor of class HallOfFame, using the arguments obtained from the line in the file. Add this new HallOfFame object to tempHallOfFameArr.
This is all to be done from the command line.
I know that my current code will not work, I was just trying to figure out some way to do this. Vectors are completely new to me, along with BufferedReader, and I've been trying to use the examples on javapractice.com as well as a few other sites for reference. I know it will be something small that I am overlooking/missing and Ill have a duh moment when I figure it out.
At any rate my current question is this.
How do I read in from a file that has "any number of white spaces/tabs" as the delimiter. And then parse that into the appropriate fields within the appropriate class?
Giving me the code isn't gonna help, if you could just point me to a website or link that I can read to have my duh moment that would be great.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;
public class HW4 {
/**
* #param args
*/
public static void main(String[] args) {
FileReader inputFileA = new FileReader("");
FileReader inputFileB = new FileReader("");
BufferedReader inputB = new BufferedReader(inputFileB);
Vector<HallOfFameMember> tempHallOfFameMemberVec = new Vector<HallOfFameMember>();
try{
BufferedReader inputA = new BufferedReader(inputFileA);
try {
String line = null;
while ((line = inputA.readLine()) != null){
}
}
}
String newHallOfFameLine = inputB.toString();
String delims = "[ \t]";
HallOfFame[] tempHallOfFameArr = newHallOfFameLine.split(delims);
for (int i = 0; i < args.length; i += 5) {
tempHallOfFameArr[i/5].setHallOfFameId(Integer.parseInt(args[i]));
tempHallOfFameArr[i/5].setCity(args[i+1]);
tempHallOfFameArr[i/5].setCostToVisit(Integer.parseInt(args[i+2]));
tempHallOfFameArr[i/5].setNumberOfVisitorsPerYear(Integer.parseInt(args[i+3]));
tempHallOfFameArr[i/5].setName(args[i+4]);
}
}
class HallOfFameMember {
private String firstName;
private String lastName;
private boolean deceased;
private int dateOfBirth;
private double totalEarned;
private int yearsPlayed;
private int yearInducted;
private int HallOfFameId;
public HallOfFameMember() {
}
public HallOfFameMember(String firstName, String lastName,
boolean deceased, int day, int month, int year, double totalEarned,
int yearsPlayed, int yearInducted, int hallOfFameId) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.deceased = deceased;
this.dateOfBirth = month + day + year;
this.totalEarned = totalEarned;
this.yearsPlayed = yearsPlayed;
this.yearInducted = yearInducted;
HallOfFameId = hallOfFameId;
}
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 boolean isDeceased() {
return deceased;
}
public void setDeceased(boolean deceased) {
this.deceased = deceased;
}
public int getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(int dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public double getTotalEarned() {
return totalEarned;
}
public void setTotalEarned(double totalEarned) {
this.totalEarned = totalEarned;
}
public int getYearsPlayed() {
return yearsPlayed;
}
public void setYearsPlayed(int yearsPlayed) {
this.yearsPlayed = yearsPlayed;
}
public int getYearInducted() {
return yearInducted;
}
public void setYearInducted(int yearInducted) {
this.yearInducted = yearInducted;
}
public int getHallOfFameId() {
return HallOfFameId;
}
public void setHallOfFameId(int hallOfFameId) {
HallOfFameId = hallOfFameId;
}
public double averageYearlySalary(double averageYearlySalary) {
return averageYearlySalary = (totalEarned / yearsPlayed);
}
}
class Date {
private int month;
private int day;
private int year;
public Date(int month, int day, int year) {
super();
this.month = month;
this.day = day;
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
class HallOfFame {
private int hallOfFameId;// ID of the hall of fame
private String name;// Name of the hall of fame
private String city;// the city in which the hall of fame is located
private double costToVisit;// cost in dollars for a visitor to a hall of
// fame for 1 day
private int numberOfVisitorsPerYear;
private static final double maxCostToVisit = 37.50;
public HallOfFame() {
}
public HallOfFame(int hallOfFameId, String name, String city,
double costToVisit, int numberOfVisitorsPerYear) {
super();
this.hallOfFameId = hallOfFameId;
this.name = name;
this.city = city;
this.costToVisit = costToVisit;
this.numberOfVisitorsPerYear = numberOfVisitorsPerYear;
}
public int getHallOfFameId() {
return hallOfFameId;
}
public void setHallOfFameId(int hallOfFameId) {
this.hallOfFameId = hallOfFameId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public double getCostToVisit() {
if (costToVisit <= maxCostToVisit) {
return costToVisit;
} else
return maxCostToVisit;
}
public void setCostToVisit(double costToVisit) {
this.costToVisit = costToVisit;
}
public int getNumberOfVisitorsPerYear() {
return numberOfVisitorsPerYear;
}
public void setNumberOfVisitorsPerYear(int numberOfVisitorsPerYear) {
this.numberOfVisitorsPerYear = numberOfVisitorsPerYear;
}
public static double getMaxcosttovisit() {
return maxCostToVisit;
}
public double totalAnnualRevenue(double totalAnnualRevenue) {
totalAnnualRevenue += costToVisit * numberOfVisitorsPerYear;
return totalAnnualRevenue;
}
}
class ReportWriter{
private Vector<HallOfFameMember> hallOfFameMemberVec;
private HallOfFame[] hallOfFameArr;
public ReportWriter() {
}
public Vector<HallOfFameMember> getHallOfFameMemberVec() {
return hallOfFameMemberVec;
}
public void setHallOfFameMemberVec(Vector<HallOfFameMember> hallOfFameMemberVec) {
this.hallOfFameMemberVec = hallOfFameMemberVec;
}
public HallOfFame[] getHallOfFameArr() {
return hallOfFameArr;
}
public void setHallOfFameArr(HallOfFame[] hallOfFameArr) {
this.hallOfFameArr = hallOfFameArr;
}
public void displayReports(){
}
}
A couple tips:
I don't want to do your homework for you, but I can't think of a quick tutorial to point you to that covers exactly what you're doing.
You're on the right track with .split, but your delimiter expression won't work for multiple spaces/tabs. Try this:
String delims = "\\s+";
That will break up your string on any consecutive sequence of one or more whitespace characters.
Also, you need to move the split up into your while loop, as well as the creation of each HallOfFameMember object. In each iteration of the loop you want to:
Split the line that you read from the file to create an array of strings representing the values for one record.
Create a new HallOfFameMember using the values from your string array. (tempHallOfFameArr[0] for first name, tempHallOfFameArr[1] for last name, etc.)
Add the new HallOfFameMember that you created to your vector
If you'd like more detail on any of these steps, I'm happy to elaborate.

Categories