Passing arguments between classes in Java - java

I'm very stuck trying to solve an excercise consisting of a java pack being tested with some tests I find impossible to pass.
There are two classes in the pack, namely Car and Parking. The relationship between them is an aggregation - Parking is the compound and Car is the component:
Parking -parking (0...1)<>------- -cars(*) Car
-parking and -cars are the attributes which with the classes Parking and Car are respectively related. The attribute -parking can have two values, 0 or 1, and -cars is an array of undefined dimension.
A car can be assigned to one or none parkings. Likewise, a parking is compound of a variable number of parking lots.
The code is as follows:
package package;
public class Parking {
private String name; // Parking's name
private String address; // Parking's address
private int capacity; // Parking's capacity (number of cars can be parked in)
private Car[] cars; // Array of cars that can be parked
public Parking() { // Default constructor
name = "Default parking";
address = "59th Street";
capacity = 10;
cars = new Car[capacity];
}
public Car[] getCars() { // Getter of the array 'cars'
return cars;
}
public int getFirstFreeParkingLot() { // Returns first free empty slot of the
// array 'cars'. A car can be parked there.
// Otherwise, returns -1
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] == null) {
b = true;
break;
}
}
if (!b) return -1;
else return i;
}
public void addCar (Car car) throws Exception { // Adds a car to a parking lot
if (car == null) throw new Exception("[ERROR] The car cannot be null");
else if (getParkingLotNumberByCar(car) != -1) throw new Exception("[ERROR] This car is already in this parking");
else if (isFull()) throw new Exception("[ERROR] This parking is full");
else if (getFirstFreeParkingLot() != -1) {
cars[getFirstFreeParkingLot()] = car;
car.setParking(car.getParking());
}
}
public void removeCar (Car car) throws Exception { // remove a car from a parking
// lot
if(getParkingLotNumberByCar(car) != -1) {
cars[getParkingLotNumberByCar(car)] = null;
car.setParking(null);
}
else throw new Exception("[ERROR] This car does not exist in this parking");
}
public boolean isFull() { // Checks if the parking is full.
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] == null) {
b = true;
break;
}
}
return !b;
}
public boolean isFree() { // Checks if there's at least one empty parking lot
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] == null) {
b = true;
break;
}
}
return b;
}
public boolean isEmpty() { // Checks if the entire parking lot is empty
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] != null) {
b = true;
break;
}
}
return !b;
}
public int getParkingLotNumberByCar (Car car) { // Return the index of the array
// 'cars' where the car given as
// argument is present in the
// parking lot. Otherwise, returns -1
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] == car) {
b = true;
break;
}
}
if (!b) return -1;
else return i;
}
public int getNumFreeParkingLots() { // Return the number of free parking lots in a parking
int i;
int n = 0;
for (i = 0; i < cars.length; i++) {
if (cars[i] == null) n++;
}
return n;
}
}
package package;
import javax.management.ObjectName;
import java.time.LocalDate;
import java.util.Objects;
import java.util.UUID;
public class Car {
private Parking parking;
public Car() {
parking = null;
}
public Parking getParking() {
return parking;
}
public void setParking(Parking parking) throws Exception {
if (parking == null)
this.parking = null;
else {
parking.addCar(parking.getCars()[parking.getParkingLotNumberByCar(new Car())]);
if (this.parking != parking) this.parking.removeCar(parking.getCars()[parking.getParkingLotNumberByCar(new Car())]);
}
}
}
And here's one test which I don't pass as an example:
package package;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import static org.junit.jupiter.api.Assertions.*;
#TestInstance(Lifecycle.PER_CLASS)
#TestMethodOrder(OrderAnnotation.class)
class IntegrationTest {
Parking parking1;
Car car1;
#BeforeAll
void init(){
try {
parking1 = new Parking();
car1 = new Car();
} catch (Exception e) {
e.printStackTrace();
fail("Init failed");
}
}
#Test
#Order(1)
void testIntegration1() {
try {
parking1.addCar(car1);
assertEquals(0, parking1.getParkingLotNumberByCar(car1));
assertEquals(9, parking1.getNumFreeParkingLots());
assertEquals(1, parking1.getFirstFreeParkingLot());
assertEquals(car1, parking1.getCars()[parking1.getParkingLotNumberByCar(car1)]);
assertEquals(parking1, car1.getParking());
} catch (Exception e) {
e.printStackTrace();
fail("Integration1 failed");
}
}
The critical part comes when utilizing the setParking (Parking parking) method in the Car class and the addCar (Car car) and removeCar (Car car) methods in the Parking class. The setParking method in the Car class establishes the attribute parking and also calls the methods addCar and remove Car of the Parking class, which in turn add to and remove a car from a parking, and finally call the mentioned setParking method.
I get to add a Car to a parking, but I fail in adding the parking's information to that car.
To refer to a Car object in the Car class, I use 'new Car', and when it comes to the Parking class, I use 'new Parking'. Am I proceeding correctly?
All the tests I'm failing at are related with the unsuccessful intertwining of these above-mentioned methods - parking.addCar, parking.removeCar, car.setParking.
Could somebody help me?
Thank you a lot in advance

I finally found a solution.
public class Parking {
private String name; // Parking's name
private String address; // Parking's address
private int capacity; // Parking's capacity (number of cars can be parked in)
private Car[] cars; // Array of cars that can be parked
public Parking() { // Default constructor
name = "Default parking";
address = "59th Street";
capacity = 10;
cars = new Car[capacity];
}
public Car[] getCars() { // Getter of the array 'cars'
return cars;
}
public int getFirstFreeParkingLot() { // Returns first free empty slot of the
// array 'cars'. A car can be parked there.
// Otherwise, returns -1
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] == null) {
b = true;
break;
}
}
if (!b) return -1;
else return i;
}
public void addCar (Car car) throws Exception {
if (car == null) throw new Exception("[ERROR] The car cannot be null");
else if ((getParkingLotNumberByCar(car) != -1)) throw new Exception("[ERROR] This car is already in this parking");
else if (isFull()) throw new Exception("[ERROR] This parking is full");
else if (isFree()) {
cars[getFirstFreeParkingLot()] = car;
car.setParking(this);
}
}
public void removeCar (Car car) throws Exception {
if(getParkingLotNumberByCar(car) != -1) {
cars[getParkingLotNumberByCar(car)] = null;
car.setParking(null);
}
else throw new Exception("[ERROR] This car does not exist in this parking");
}
public boolean isFull() { // Checks if the parking is full.
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] == null) {
b = true;
break;
}
}
return !b;
}
public boolean isFree() { // Checks if there's at least one empty parking lot
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] == null) {
b = true;
break;
}
}
return b;
}
public boolean isEmpty() { // Checks if the entire parking lot is empty
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] != null) {
b = true;
break;
}
}
return !b;
}
public int getParkingLotNumberByCar (Car car) { // Return the index of the array
// 'cars' where the car given as
// argument is present in the
// parking lot. Otherwise, returns -1
int i;
boolean b = false;
for (i = 0; i < cars.length; i++) {
if (cars[i] == car) {
b = true;
break;
}
}
if (!b) return -1;
else return i;
}
public int getNumFreeParkingLots() { // Return the number of free parking lots in a parking
int i;
int n = 0;
for (i = 0; i < cars.length; i++) {
if (cars[i] == null) n++;
}
return n;
}
}
public class Car {
private Parking parking;
public Car() {
parking = null;
}
public Parking getParking() {
return parking;
}
public void setParking(Parking parking) throws Exception {
if (parking != null) {
if (parking.getParkingLotNumberByCar(this) == -1) {
parking.addCar(this);
}
if (this.parking != null && this.parking != parking) {
this.parking.removeCar(this);
}
this.parking = parking;
}
else {
if (this.parking != null) {
if (this.parking.getParkingLotNumberByCar(this) != -1)
this.parking.removeCar(this);
}
this.parking = null;
}
}
}

Related

Confusion on using instanceof along with other inherited data

I have already made a posting about this program once, but I am once again stuck on a new concept that I am learning (Also as a side note; I am a CS student so please DO NOT simply hand me a solution, for my University has strict code copying rules, thank you.). There are a couple of difficulties I am having with this concept, the main one being that I am having a hard time implementing it to my purposes, despite the textbook examples making perfect sense. So just a quick explanation of what I'm doing:
I have an entity class that takes a Scanner from a driver. My other class then hands off the scanner to a superclass and its two subclasses then inherit that scanner. Each class has different data from the .txt the Scanner read through. Then those three classes send off their data to the entity to do final calculations. And that is where my problem lies, after all the data has been read. I have a method that displays a new output along with a few methods that add data from the super along with its derived classes.EDIT: I simply cannot figure out how to call the instance variable of my subclasses through the super so I can add and calculate the data.
Here are my four classes in the order; Driver, Entity, Super, Subs:
public static final String INPUT_FILE = "baseballTeam.txt";
public static void main(String[] args) {
BaseballTeam team = new BaseballTeam();
Scanner inFile = null;
try {
inFile = new Scanner(new File(INPUT_FILE));
team.loadTeam(inFile);
team.outputTeam();
} catch (FileNotFoundException e) {
System.out.println("File " + INPUT_FILE + " Not Found.");
System.exit(1);
}
}
}
public class BaseballTeam {
private String name;
private Player[] roster = new Player[25];
Player pitcher = new Pitcher();
Player batter = new Batter();
BaseballTeam() {
name = "";
}
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
public void loadTeam(Scanner input) {
name = input.nextLine();
for (int i = 0; i < roster.length; i++) {
if (i <= 9) {
roster[i] = new Pitcher();
}
else if ((i > 9) && (i <= 19)) {
roster[i] = new Batter();
}
else if (i > 19) {
roster[i] = new Player();
}
roster[i].loadData(input);
roster[i].generateDisplayString();
//System.out.println(roster[i].generateDisplayString()); //used sout to test for correct data
}
}
public void outputTeam() {
if ((pitcher instanceof Player) && (batter instanceof Player)) {
for (int i = 0; i < roster.length; i++) {
System.out.println(roster[i].generateDisplayString());
}
}
//How do I go about doing calculates?
public int calculateTeamWins() {
if ((pitcher instanceof ) && (batter instanceof Batter)) {
}
return 0;
}
public int calculateTeamSaves() {
if ((pitcher instanceof Pitcher) && (batter instanceof Batter)) {
}
return 0;
}
public double calculateTeamERA() {
if ((pitcher instanceof Pitcher) && (batter instanceof Batter)) {
}
return 0;
}
public double calculateTeamWHIP() {
if ((pitcher instanceof Pitcher) && (batter instanceof Batter)) {
}
return 0;
}
public double calculateTeamBattingAverage() {
if ((pitcher instanceof Pitcher) && (batter instanceof Batter)) {
}
return 0;
}
public int calculateTeamHomeRuns() {
if ((pitcher instanceof Pitcher) && (batter instanceof Batter)) {
}
return 0;
}
public int calculateTeamRBI() {
if ((pitcher instanceof Pitcher) && (batter instanceof Batter)) {
}
return 0;
}
public int calculateStolenBases() {
if ((pitcher instanceof Pitcher) && (batter instanceof Batter)) {
}
return 0;
}
}
public class Player {
protected String name;
protected String position;
Player(){
name = "";
position = "";
}
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
public String getPosition() {
return position;
}
public void setPosition(String aPosition) {
position = aPosition;
}
public void loadData(Scanner input){
do {
name = input.nextLine();
} while (name.equals(""));
position = input.next();
//System.out.println(generateDisplayString());
}
public String generateDisplayString(){
return "Name: " + name + ", Position:" + position;
}
}
public class Pitcher extends Player {
private int wins;
private int saves;
private int inningsPitched;
private int earnedRuns;
private int hits;
private int walks;
private double ERA;
private double WHIP;
Pitcher() {
super();
wins = 0;
saves = 0;
inningsPitched = 0;
earnedRuns = 0;
hits = 0;
walks = 0;
}
public int getWins() {
return wins;
}
public void setWins(int aWins) {
wins = aWins;
}
public int getSaves() {
return saves;
}
public void setSaves(int aSaves) {
saves = aSaves;
}
public int getInningsPitched() {
return inningsPitched;
}
public void setInningsPitched(int aInningsPitched) {
inningsPitched = aInningsPitched;
}
public int getEarnedRuns() {
return earnedRuns;
}
public void setEarnedRuns(int aEarnedRuns) {
earnedRuns = aEarnedRuns;
}
public int getHits() {
return hits;
}
public void setHits(int aHits) {
hits = aHits;
}
public int getWalks() {
return walks;
}
public void setWalks(int aWalks) {
walks = aWalks;
}
#Override
public void loadData(Scanner input) {
super.loadData(input);
wins = input.nextInt();
saves = input.nextInt();
inningsPitched = input.nextInt();
earnedRuns = input.nextInt();
hits = input.nextInt();
walks = input.nextInt();
}
#Override
public String generateDisplayString() {
calculateERA();
calculateWHIP();
return String.format(super.generateDisplayString() + ", Wins:%1d, Saves:%1d,"
+ " ERA:%1.2f, WHIP:%1.3f ", wins, saves, ERA, WHIP);
}
public double calculateERA() {
try {
ERA = ((double)(earnedRuns * 9) / inningsPitched);
} catch (ArithmeticException e) {
ERA = 0;
}
return ERA;
}
public double calculateWHIP() {
try {
WHIP = ((double)(walks + hits) / inningsPitched);
} catch (ArithmeticException e) {
WHIP = 0;
}
return WHIP;
}
}
public class Batter extends Player {
private int atBats;
private int hits;
private int homeRuns;
private int rbi;
private int stolenBases;
private double batAvg;
Batter() {
super();
atBats = 0;
hits = 0;
homeRuns = 0;
rbi = 0;
stolenBases = 0;
}
public int getAtBats() {
return atBats;
}
public void setAtBats(int aAtBats) {
atBats = aAtBats;
}
public int getHits() {
return hits;
}
public void setHits(int aHits) {
hits = aHits;
}
public int getHomeRuns() {
return homeRuns;
}
public void setHomeRuns(int aHomeRuns) {
homeRuns = aHomeRuns;
}
public int getRbi() {
return rbi;
}
public void setRbi(int aRbi) {
rbi = aRbi;
}
public int getStolenBases() {
return stolenBases;
}
public void setStolenBases(int aStolenBases) {
stolenBases = aStolenBases;
}
#Override
public void loadData(Scanner input) {
super.loadData(input);
atBats = input.nextInt();
hits = input.nextInt();
homeRuns = input.nextInt();
rbi = input.nextInt();
stolenBases = input.nextInt();
}
#Override
public String generateDisplayString() {
calculateBattingAverage();
return String.format(super.generateDisplayString() +
", Batting Average:%1.3f, Home Runs:%1d, RBI:%1d, Stolen Bases:%1d"
, batAvg, homeRuns, rbi, stolenBases);
}
public double calculateBattingAverage() {
try{
batAvg = ((double)hits/atBats);
} catch (ArithmeticException e){
batAvg = 0;
}
return batAvg;
}
}
Also, its probably easy to tell I'm still fairly new here, because I just ran all my classes together in with the code sample and I can't figure out to add the gaps, so feel free to edit if need be.
The typical usage of instanceof in the type of scenario you're describing would be
if (foo instanceof FooSubclass) {
FooSubclass fooSub = (FooSubclass) foo;
//foo and fooSub now are references to the same object, and you can use fooSub to call methods on the subclass
} else if (foo instanceof OtherSubclass) {
OtherSubclass otherSub = (OtherSubclass) foo;
//you can now use otherSub to call subclass-specific methods on foo
}
This is called "casting" or "explicitly casting" foo to FooSubclass.
the concept to call the methods of your subclasses is called polymorphism.
In your runtime the most specific available method is called provided that the method names are the same.
so you can
Superclass class = new Subclass();
class.method();
and the method provided that overwrites the method in Superclass will be called, even if it's defined in the Subclass.
Sorry for my english, I hope that helps a little bit ;-)

Exception in thread "main" java.lang.NullPointerException virtual stack class

I am trying to use this class to evaluate postfix expressions and when testing it am thrown this exception at line 32 where the virtual stack should push.
public class PostfixEval
{
private IntStack s;
public PostfixEval()
{
IntStack s = new IntStack();
}
public boolean isInteger(String s)
{
int i = 0;
boolean isDigit = true;
while(i < s.length() && isDigit)
{
isDigit = s.charAt(i) >= '0' && s.charAt(i) <= '9';
i++;
}
return isDigit;
}
public int eval(String e)
{
String[] tokens = e.split("\\s+");
for(int i=0; i<tokens.length; i++)
{
if(isInteger(tokens[i]))
{
s.push(Integer.parseInt(tokens[i]));
}
else
{
int a,b,c;
b = s.pop();
a = s.pop();
c = 0;
char d = tokens[i].charAt(0);
if(d == '+')
{
c = a + b;
}
else if(d == '-')
{
c = a - b;
}
else if(d == '*')
{
c = a*b;
}
else if(d == '/')
{
c = a/b;
}
else if(d == '%')
{
c = a%b;
}
else
{
System.out.println("Error");
System.exit(0);
}
s.push(c);
}
}
return s.peek();
}
}
I have used jgrasp to see what Integer.parseInt(tokens[i])) evaluates to and confirm it is a number from the split string. When trying to push a number that I type into the paramater of the push method it works, so why do I get null exception when using the PostfixEval to push?
Here is my stack class.
public class IntStack implements StackIntADT
{
// fields
private int[] stk;
private int sp;
// constructors
public IntStack()
{
sp = -1;
stk = new int[10];
}
public IntStack( int s )
{
sp = -1;
stk = new int[s];
}
// stack class methods
public void push(int element)
{
if(!isFull())
{
sp++;
stk[sp]=element;
}
else
{
System.out.println("Element" + element);
System.exit(0);
}
}
public int pop()
{
int rv = 0;
if(!isEmpty())
{
rv = stk[sp--];
}
else
{
System.out.println(rv);
System.exit(0);
}
return rv;
}
public int peek()
{
return stk[sp];
}
public boolean isEmpty()
{
return sp==-1;
}
public boolean isFull()
{
return sp==stk.length-1;
}
public int size()
{
return stk.length;
}
public String toString()
{
String s = "";
for(int x=0;x<10;x++)
{
s = s + " " + stk[x];
}
return s;
}
}
The constructor should not define a local s variable (which hides the member variable with the same name). The member variable is never assigned a value.
Change the constructor to this:
public PostfixEval() {
s = new IntStack();
}

ArrayList comparison

I have an ArrayList made out of classes objects .The class has several String fields . In some classes
some fields that are the same must be removed from the ArrayList .
The field from the class that I need to check is sorted_participants which is set to be the same in some objects.
This is my Class:
public class Neo4jCompensation {
private String number_of_participants;
private String amount;
private String sorted_participants;
private String unsorted_participants;
private String href_pdf_compensation;
private String signed_by_all;
public Neo4jCompensation() {
this.number_of_participants = "";
this.amount = "";
this.sorted_participants = "";
this.unsorted_participants = "";
this.href_pdf_compensation = "";
this.signed_by_all = "";
}
public String getNumber_of_participants() {
return number_of_participants;
}
public void setNumber_of_participants(String number_of_participants) {
this.number_of_participants = number_of_participants;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getSorted_participants() {
return sorted_participants;
}
public void setSorted_participants(String sorted_participants) {
this.sorted_participants = sorted_participants;
}
public String getUnsorted_participants() {
return unsorted_participants;
}
public void setUnsorted_participants(String unsorted_participants) {
this.unsorted_participants = unsorted_participants;
}
public String getHref_pdf_compensation() {
return href_pdf_compensation;
}
public void setHref_pdf_compensation(String href_pdf_compensation) {
this.href_pdf_compensation = href_pdf_compensation;
}
public String getSigned_by_all() {
return signed_by_all;
}
public void setSigned_by_all(String signed_by_all) {
this.signed_by_all = signed_by_all;
}
}
So I have a first list filled with Classes:
ArrayList<Neo4jCompensation> results_list=new ArrayList<Neo4jCompensation>();
I thought that very good way to find the duplicates is to make a copy of a list , compare the two for the same class fields values and remove the duplicates .
This is how I find the duplicates
ArrayList<Neo4jCompensation> results_list1=new ArrayList<Neo4jCompensation>();
for(Neo4jCompensation pp:results_list)
{
Neo4jCompensation ss=new Neo4jCompensation();
ss.setAmount(pp.getAmount());
ss.setHref_pdf_compensation(pp.getHref_pdf_compensation());
ss.setNumber_of_participants(pp.getNumber_of_participants());
ss.setSigned_by_all(pp.getSigned_by_all());
ss.setSorted_participants(pp.getSorted_participants());
ss.setUnsorted_participants(pp.getUnsorted_participants());
results_list1.add(ss);
}
for (int i = 0; i < results_list.size(); i++) {
Neo4jCompensation kk=new Neo4jCompensation();
kk=results_list.get(i);
for (int j = 0; j < results_list1.size(); j++) {
Neo4jCompensation n2=new Neo4jCompensation();
n2=results_list1.get(j);
if(i!=j)
{
String prvi=kk.getSorted_participants().trim();
String drugi=n2.getSorted_participants().trim();
if(prvi.equals(drugi))
{
// results_list1.remove(j);
out.println("<p> Are equal su :"+i+" i "+j+"</p>");
}
}
}
}
Since I know that I can not loop and remove the elements from the ArrayList at the same
time i tried to use iterators like this ...
int one=0;
int two=0;
Iterator<Neo4jCompensation> it = results_list.iterator();
while (it.hasNext()) {
Neo4jCompensation kk=it.next();
Iterator<Neo4jCompensation> it1 = results_list1.iterator();
while (it1.hasNext()) {
Neo4jCompensation kk1=it1.next();
String oo=kk.getSorted_participants().trim();
String pp=kk1.getSorted_participants().trim();
if(one<two && oo.equals(pp))
{
it1.remove();
}
two++;
}
one++;
}
But it fails and gives me back nothing in ArrayList results_list1 - before removal with iterator it has in it the right elements . How to remove the objects from the array list that have the same field values as some other objects in the ArrayList .
Why not use .removeAll()
results_list1.removeAll(resultList);
This will require you to implement equals and hashcode within Neo4jCompensation
public class Neo4jCompensation {
//Omitted Code
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime
* result
+ ((number_of_participants == null) ? 0
: number_of_participants.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Neo4jCompensation other = (Neo4jCompensation) obj;
if (number_of_participants == null) {
if (other.number_of_participants != null)
return false;
} else if (!number_of_participants.equals(other.number_of_participants))
return false;
return true;
}
}
Another approach could be:
Overrride equals method in your Neo4jCompensation and check for the sorted_participants and return accordingly. Then use Set as a collection of your objects. Set does not allow duplicates and it uses equals to determine the equality.
for (int i = 0; i < results_list.size(); i++) {
Neo4jCompensation kk=new Neo4jCompensation();
kk=results_list.get(i);
for (int j = 0; j < results_list1.size(); j++) {
Neo4jCompensation n2=new Neo4jCompensation();
n2=results_list1.get(j);
if(results_list1.contains(kk) { results_list1.remove(j); }
}
But you may want to use a Set instead of a List or a data structure that maintains order, like a SortedMap, with a sentinel value.

Stack Overflow error adding an object in ArrayList of []

I get stack overflow error when I try to load(). I am trying to read PhoneBookEntry objects from a txt file. The txt file has a name and number which make up the PhoneBookEntry object.
Can you please let me know what I'm doing wrong?
package HashSet;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Scanner;
public class PhoneBook {
int capacity = 10;
private ArrayList<PhoneBookEntry>[] buckets;
public PhoneBook() {
this(10);
}
public PhoneBook(int size) {
capacity = size;
buckets = new ArrayList[size];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new ArrayList<PhoneBookEntry>();
}
}
public int getSize() {
int tot = 0;
for (ArrayList<PhoneBookEntry> x : buckets)
tot += x.size();
return tot;
}
public boolean add(PhoneBookEntry entry) {
if (contains(entry))
return false;
int x = Math.abs(entry.hashCode());
buckets[x % buckets.length].add(entry);
return true;
}
public void load()
{
InputStream is = getClass().getClassLoader().getResourceAsStream("phone.txt");
Scanner scan = new Scanner(is);
if (scan.hasNext())
add(new PhoneBookEntry(scan.next());
}
public void bucketSize() {
for (int i = 0; i < buckets.length; i++)
System.out.println(i + " " + buckets[i].size());
}
public boolean contains(PhoneBookEntry word) {
int x = Math.abs(word.hashCode());
return buckets[x % buckets.length].contains(word);
}
public int getCapacity() {
return capacity;
}
public static void main(String[] args) {
PhoneBook phone = new PhoneBook();
phone.load();
}
}
package HashSet;
import java.util.LinkedList;
import java.util.ListIterator;
public class PhoneBookEntry {
String n;
Integer nr;
LinkedList<PhoneBookEntry> list;
public PhoneBookEntry(String name, int number) {
list = new LinkedList<PhoneBookEntry>();
n = name;
nr = number;
list.add(new PhoneBookEntry(n, nr));
}
public String getN() {
return n;
}
public void setN(String n) {
this.n = n;
}
public Integer getNr() {
return nr;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((n == null) ? 0 : n.hashCode());
result = prime * result + ((nr == null) ? 0 : nr.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PhoneBookEntry other = (PhoneBookEntry) obj;
if (n == null) {
if (other.n != null)
return false;
} else if (!n.equals(other.n))
return false;
if (nr == null) {
if (other.nr != null)
return false;
} else if (!nr.equals(other.nr))
return false;
return true;
}
public void setNr(Integer nr) {
this.nr = nr;
}
#Override
public String toString() {
return n + " " + nr;
}
}
Every new phone book entry creates a new phone book entry that creates a new phone book entry that creates a new phome book entry, etc... ad infinitum. That is, until the stack space runs out.
You need to rethink your application data structure.
public PhoneBookEntry(String name, int number) {
list = new LinkedList<PhoneBookEntry>();
n = name;
nr = number;
list.add(new PhoneBookEntry(n, nr));
}
is causing an infinite recursion. You likely need a new class to put in the linked list (PhoneNumber or some such if a PhoneBookEntry can contain multiple names/numbers, otherwise ditch it.)

Generic Class Iterator

I have three classes, those being Lister, ObjectSortedList and SortedListProgram. I'm having trouble with the iterator for the generic class. What am I doing wrong?
This is the error I get:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at objectsortedlist.ObjectSortedList.getData(ObjectSortedList.java:122)
at objectsortedlist.Lister.hasNext(Lister.java:28)
at objectsortedlist.SortedListProgram.main(SortedListProgram.java:52)
Java Result: 1
Here are my classes:
package objectsortedlist;
import java.util.Iterator;
/**
*
* #author Steven
*/
public class ObjectSortedList<T> implements Cloneable, Iterable<T> {
private T[] data;
private int capacity;
public ObjectSortedList()
{
final int init_capacity = 10;
capacity = 0;
data = (T[])new Object[init_capacity];
}
public ObjectSortedList(int init_capacity)
{
if(init_capacity < 0)
throw new IllegalArgumentException("Initial capacity is negative: " + init_capacity);
capacity = 0;
data = (T[])new Object[init_capacity];
}
private boolean empty()
{
if(data.length == 0 || data[0] == null)
return true;
else
return false;
}
public int length()
{
return capacity;
}
public void insert(T element)
{
if(capacity == data.length)
{
ensureCapacity(capacity * 2 + 1);
}
data[capacity] = element;
capacity++;
}
public boolean delete(T target)
{
int index;
if(target == null)
{
index = 0;
while((index < capacity) && (data[index] != null))
index++;
}
else
{
index = 0;
while((index < capacity) && (!target.equals(data[index])))
index++;
}
if(index == capacity)
return false;
else
{
capacity--;
data[index] = data[capacity];
data[capacity] = null;
return true;
}
}
private void ensureCapacity(int minCapacity)
{
T[] placeholder;
if(data.length < minCapacity)
{
placeholder = (T[])new Object[minCapacity];
System.arraycopy(data, 0, placeholder, 0, capacity);
data = placeholder;
}
}
public ObjectSortedList<T> clone()
{
// Cloning
ObjectSortedList<T> answer;
try
{
answer = (ObjectSortedList<T>) super.clone();
}
catch(CloneNotSupportedException cnse)
{
throw new RuntimeException("This class does not implement cloneable.");
}
answer.data = data.clone();
return answer;
}
#Override
public Iterator<T> iterator()
{
return (Iterator<T>) new Lister<T>(this, 0);
}
public T getData(int index)
{
return (T)data[index];
}
}
package objectsortedlist;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
*
* #author Steven
*/
public class Lister<T> implements Iterator<T>
{
private ObjectSortedList<T> current;
private int index;
public Lister(ObjectSortedList<T> top, int index)
{
current = top;
this.index = index;
}
#Override
public boolean hasNext()
{
return (current.getData(index) == null);
}
#Override
public T next()
{
T answer;
if(!hasNext())
throw new NoSuchElementException("The Lister is empty.");
answer = current.getData(index+1);
return answer;
}
#Override
public void remove() {
throw new UnsupportedOperationException("Don't use this. Use objectsortedlist.SortedList.delete(T target).");
}
}
package objectsortedlist;
import java.util.Scanner;
/**
*
* #author Steven
*/
public class SortedListProgram {
private static Scanner scan = new Scanner(System.in);
private static String[] phraseArray = {"Hullabaloo!", "Jiggery pokery!", "Fantastic!", "Brilliant!", "Clever!", "Geronimo!", "Fish sticks and custard.", "Spoilers!",
"Exterminate!", "Delete!", "Wibbly-wobbly!", "Timey-wimey!"};
private static Lister<String> print;
public static void main(String args[])
{
int phraseNo = 0;
System.out.println("I'm gonna say some things at you, and you're going to like it."
+ " How many things would you like me to say to you? Put in an integer from 1-12, please.");
try
{
phraseNo = Integer.parseInt(scan.nextLine());
while((phraseNo < 1) || (phraseNo > 12))
{
System.out.println("The integer you entered wasn't between 1 and 12. Make it in between those numbers. Please? Pleaseeeee?");
phraseNo = Integer.parseInt(scan.nextLine());
}
}
catch(NumberFormatException nfe)
{
System.out.println("C'mon, why don't you follow directions?");
phraseNo = 0;
}
if(phraseNo == 0);
else
{
ObjectSortedList<String> phrases = new ObjectSortedList<String>(phraseNo);
for(int i = 0; i < phrases.length(); i++)
{
phrases.insert(phraseArray[i]);
}
print = new Lister<String>(phrases, phraseNo);
while(print.hasNext())
System.out.println(print.next());
}
}
}
After looking at your code I found multiple issues, here are they:
In your SortedListProgram class, in following code the phrases.length() will be 0, so the it will never go in that loop.
ObjectSortedList<String> phrases = new ObjectSortedList<String>(phraseNo);
for(int i = 0; i < phrases.length(); i++)
{
phrases.insert(phraseArray[i]);
}
Moreover in SortedListProgram class's this call sequence
print.hasNext() -> current.getData(index)
the index passed is equal to size of data array field in the
ObjectSortedList class and Since in java array indexes ranges from
zero to array size -1. So you are bound to get
java.lang.ArrayIndexOutOfBoundsException always.
Please correct your code.

Categories

Resources