In Main:
Equipe Eq1 = new Equipe(J,E);
Equipe Eq2 = new Equipe(J,E);
while(Eq1.equals(Eq2))
Eq2 = new Equipe(J,E);
Match m = new Match(Eq1,Eq2);
String ChercherJoueur = m.QuelEquipe(m.hasBall());
In Class Equipe:
public Vector<Joueur> VJ;
public Equipe(Vector<Joueur> E, Vector<Entraineur> Ent) {
VJ = new Vector<Joueur>();
//rest of the logic
}
public Equipe() {
}
In Class Match:
Equipe Eq1 = new Equipe();
Equipe Eq2 = new Equipe();
public Match(Equipe Eq1, Equipe Eq2) {
Eq1 = this.Eq1;
Eq2 = this.Eq2;
}
public String QuelEquipe(Joueur J)
{
boolean found = Eq1.ChercherJoueur(J);
if(found == true)
return "EQ1";
else
return "EQ2";
}
public Joueur hasBall()
{
Joueur J = null;
int i = 0;
boolean found = false;
NullPointerException-------> System.out.println(Eq1.VJ.get(i).isBall());
System.out.println(Eq2.VJ.get(i).isBall());
while(!found)
{
if((Eq1.VJ.get(i).isBall())==true)
{
found = true;
J= Eq1.VJ.get(i);
}
else if((Eq2.VJ.get(i).isBall())==true)
{
found = true;
J= Eq2.VJ.get(i);
}
i++;
}
return J;
}
}
I think is all I need to include here to inform u about the situation.. I get a NullPointerException when I do "m.QuelEquipe(m.hasBall());" that can be traced back to where I pointed in The Class Match.. I know exactly what the exception means, and I'm changing their references with "Eq1 = this.Eq1;Eq2 = this.Eq2;" anyway.. sooo where is flow to get the code to work??
Your constructor using two args does initialize the vector but the parameterless constructor doesn't
public Equipe(Vector<Joueur> E, Vector<Entraineur> Ent) {
VJ = new Vector<Joueur>(); //<-- OK
//rest of the logic
}
public Equipe() {
//<-- errrk
}
Then when you invoke:
System.out.println(Eq2.VJ.get(i).isBall());
You're using really invoking:
Eq2.null.get <-- NullPointerException
But the real problem though is in the Match constructor:
public Match(Equipe Eq1, Equipe Eq2) {
Eq1 = this.Eq1;
Eq2 = this.Eq2;
}
Here you're assigning to the local variable Eq1 the value of the instance variable Eq1 you really want it the other way around:
public Match(Equipe Eq1, Equipe Eq2) {
this.Eq1 = Eq1;
this.Eq2 = Eq2;
}
BTW, this is not C# and in Java as a coding conventions both, methods and attributes start with lowecase and opening brace goes in the same line ( although this last part is not as relevant as the naming convention )
I hope this helps.
You are calling the default constructor public Equipe() which does not initialize VJ. Just remove the constructor if you are not going to use it.
This will fix your problem:
Match.java
private final Equipe eq1;
private final Equipe eq2;
public Match(final Equipe eq1, final Equipe eq2)
{
this.eq1 = eq1;
this.eq2 = eq2;
}
Related
import java.util.ArrayList;
import java.util.List;
public class QueryParser {
QueryParameter queryParameter = new QueryParameter();
public static int flag = 0;
public QueryParameter parseQuery(String queryString) {
queryParameter.setGroupByFields(getGroupByFields(queryString));
queryParameter.setAggregateFunctions(getAggregateFunctions(queryString));
return queryParameter;
}
private List<AggregateFunction> getAggregateFunctions(String queryString) {
List<AggregateFunction> aggregateFunctionList = null;
AggregateFunction aggregateFunction;
if (queryString.contains("count") || queryString.contains("sum") || queryString.contains("min")
|| queryString.contains("max") || queryString.contains("avg")) {
flag = flag + 1;
}
return aggregateFunctionList;
}
private List<String> getGroupByFields(String queryString) {
List<String> groupByFieldList = null;
if (queryString.contains("group by")) {
flag = flag+2;
return groupByFieldList;
}
}
this is my code, now i am accessing flag from another class using
int i = queryParser.flag,
but it always returns 0. How to declare flag so that it will hold the values from the inner methods and also can be accessed from other classes. /* if anyone need more details please ask i'll add more details, and what more should i write,my main doubt is only to know how i can utilise my flag in another class, thank you for being patient*/
An MCVE could look like this:
public class QueryParser {
public static int flag = 0;
void getAggregateFunctions(String queryString) {
if (queryString.contains("count") || queryString.contains("sum") || queryString.contains("min")
|| queryString.contains("max") || queryString.contains("avg")) {
flag = flag + 1;
}
}
void getGroupByFields(String queryString) {
if (queryString.contains("group by")) {
flag = flag+2;
}
}
public static void main(String[] args) {
QueryParser q = new QueryParser();
System.out.println(flag); //prints out 0
q.getAggregateFunctions("max"); System.out.println(flag); //prints out 1
q.getAggregateFunctions("aString"); System.out.println(flag); //prints out 1
q.getAggregateFunctions("avgNumber"); System.out.println(flag); //prints out 2
q.getGroupByFields("group by"); System.out.println(flag); //prints out 4
}
}
It helps those who want to help you, and far more important: it is a great debugging tool.
Making on helps you isolate the problem, and focus on it.
Like in this case: you can see that the problem is not in the declaration of flag, nor changing its value, but maybe with the input values.
Is there a better way (probably..) to build a class in which i can use set/get method.
Notice thay all the data are stock in a ArrayList.
public class PorterList
{
public PorterList()
{
ArrayList<Porter> porList = new ArrayList<>();
}
public PorterList(ArrayList<Porter> p)
{
ArrayList<Porter> porList = p;
}
SimpleDateFormat porterDF = new SimpleDateFormat("HH:mm:ss");
private Porter p = new Porter();
private int _porterNo;
public String getStatus(int porterNo)
{
_porterNo = porterNo;
p = porList.get(_porterNo);
return p.p_state;
}
There's something wrong on that second last line p = porList.get(_porterNo);
I want to use something like this in my main:
p_L = PorterList(p)
porter_status = p_L.get(5)
Thank you very much
Yor ArrayList is local variable and it's your problem. It's should be a field.
private ArrayList<Porter> porList;
public PorterList() {
porList = new ArrayList<>();
}
public PorterList(ArrayList<Porter> p
{
porList = p;
}
As you declare ArrayList porList = p; inside the constructors , It will become local variable , so it will not be visible outside that constructor , If you want it to use at your class level declare it at globally liek below
public class PorterList
{
private ArrayList<Porter> porList;
public PorterList()
{
porList = new ArrayList<>();
}
public PorterList(ArrayList<Porter> p)
{
porList = p;
}
}
In both of your constructors you have declared local variable porList - make this a field
public class PorterList
{
private ArrayList<Porter> porList;
public PorterList()
{
porList = new ArrayList<>();
}
public PorterList(ArrayList<Porter> p)
{
porList = p;
}
....
}
I am studying the inheritance (Java), and I wrote the following code. The first part is the CarBase, and then I created a childclass 1, called Bus.
My idea is that first make a judgement if it is a bus, and by doing that, I need a boolean [if(isBus)], but when I wrote this code in Eclipse, there is a error message, said 'isBus can not be resolved to a variable'.
Could some one please tell me how to solve this problem? Do I need to declare the boolean variable first?
Another question is about the declaration of local variables.
In the getOnBus(0 method, I have a local variable called temp,I was taught that whenever using a local variable insided a method, I need to declare it first and then I shall be able to use it, but I saw someone use it directly like the following, I was wandering what's the difference between the two?
public class Bus extends CarBase {
//Unique bus properties
public int max_Passenger = 35;
public int current_Passenger = 0;
// unique bus method
public boolean getOnBus(int p_amount) {
if(isBus) {
int temp = 0; // <===
temp = current_Passenger + p_amount; // <===
if( temp > max_Passenger) {
return false;
} else {
current_Passenger = temp;
return true;
}
}
return false;
}
}
or if there is difference if I use it without declaring it?
public class Bus extends CarBase {
//Unique bus properties
public int max_Passenger = 35;
public int current_Passenger = 0;
// unique bus method
public boolean getOnBus (int p_amount) {
if(isBus) {
int temp=current_Passenger+p_amount; // <====
if( temp > max_Passenger) {
return false;
} else {
current_Passenger = temp;
return true;
}
}
return false;
}
}
The code is as following
First Part CarBase(parent)
public class CarBase {
public int speed;
public String name;
public String color;
public int maxSpeed = 90;
// Method
public void speedUp(int p_speed) {
int tempSpeed = 0;
if (p_speed > 0) {
tempSpeed = speed + p_speed;
}
if (tempSpeed <= maxSpeed) {
speed =tempSpeed;
}
}
}
Second Part Bus (Child1)
public class Bus extends CarBase {
//Unique bus properties
public int max_Passenger = 35;
public int current_Passenger = 0;
// unique bus method
public boolean getOnBus (int p_amount) {
if (isBus) {
int temp = 0;
temp = current_Passenger + p_amount;
if (temp > max_Passenger) {
return false;
} else {
current_Passenger = temp;
return true;
}
}
return false;
}
}
The point in using inherance is to abstract whether an object is a Car or a Bus, and write code that works no matter what is passed. To do so, you use abstract methods. Consider
abstract class Vehicle {
private int occupied;
public Vehicle() {
occupied = 0;
}
public abstract int getCapacity(); // number of passengers
public boolean board(int howmany) {
if (occupied+howmany <= capacity) {
occupied += howmany;
return true;
}
else
return false;
}
public void unboard(int howmany) {
occupied -= howmany;
}
};
class Car extends Vehicle {
public Car () { super(); } // just call the Vehicle() constructor
public int getCapacity() { return 5; }
}
class Bus extends Vehicle {
public Bus() { super(); } // just call the Vehicle() constructor
public int getCapacity() { return 32; }
}
you'd write every function to accept a Vehicle, and deal with it without the need to know if it is a bus or a car. (the following is a dumb function, just to give you an example)
void board_on_first_avaible(Vehicle[] x, int n) {
for (int i=0; i<x.length; x++)
if (x.board(n))
return true; // board ok
return false; // couldn't board on anything
}
Note that you should design your code so that the functions are declared, abstract in Vehicle, for both Car and Bus. Thus getOnBus() would be a bad idea
OK for the first point "isBus" is not declared, i can not see the point of checking in this method as you already know u are extending the CarBase but if you need to check you can do it like this
if(this instanceof CarBase)
for the second point there is actually no effect for the change
int temp=0; // <===
temp= current_Passenger+p_amount; // <===
first you initialize with 0 then you assign the new value to it
int temp=current_Passenger+p_amount;
here you initialize the temp with the value
You don't need to check if the Bus object 'isBus()' .... it IS a Bus, because you are defining the class as Bus!
So... if you were to create a new Bus object, you would say something like:
Bus BigYellowBus0001 = new Bus();
if you were to then say:
BigYellowBus0001.getOnBus(10);
You would NOT need to check if BigYellowBus0001 is a bus.... right?
In fact, you don't even need to name the method getOnBus().... it could just be getOn.
I think maybe you've gotten off on the wrong foot by deciding that Bus is a subclass of Car.
As for local variables, this just means variable that begin and end inside the method... so you did that nicely with your 'temp' variable.
To show that you understand how to access variables of the superclass from the child class, you could check the speed of the bus before letting people on:
public boolean getOnBus (int p_amount){
if(speed = 0){
int temp=0;
temp= current_Passenger+p_amount;
if( temp > max_Passenger){
return false;
} else{
current_Passenger = temp;
return true;
}
}
return false;
}
isBus is not declared that reason why you got this error
You doesn't need this check, because this method declared for Bus class and you are sure what it IS a Bus not a parent CarBase class (please use Vechicle instead of CarBase, it's much better on my opinion)
In Java 0 is default value for int, so you don't need to init variable before assign new value
So you can simplify getOnBus() like that
public boolean getOnBus (int p_amount) {
int temp = current_Passenger + p_amount;
if (temp > max_Passenger) return false;
current_Passenger = temp;
return true;
}
To test if an object is an instance of a class you can to use variable instanceof YourClass which evaluates to a boolean
I am testing a class that was implemented from an interface.
In my test class, I have created objects (Humains) that have a name and bank account value.
One of my methods to test takes an arrayList and compares with another list and if the person already exists, the amount will be added.
Code is something like that:
TEST CLASS
HumainImpl<Humain> humain = new HumainImpl<Humain>();
private Humain h1 = new Humain("AAA" , 2200);
private Humain h2 = new Humain("DDD" , 500);
#Test
public void testAddAmount(){
List<Humain> testing = new ArrayList<Humain>();
testing.add(h1);
testing.add(h2);
humain.placeList(testing);
}
the placeList(testing) method is in the HumainImpl class which calls another method in the Humain class that does the addition.
In the HumainImpl class there is also a list called implList.
Once this method placeList is called, the numbers for h1 and h2 in the test class are changing.
What I want, is that only the number for the implList to change.
Is it something normal that when adding up numbers or making changes while passing the h1 and h2 as parameter they will get effected ?
I have tried too many ways and I don't get it. I tried going through the list passed as a parameter copy the element in that list into another attribut and do the addition but it didn't help.
This works fine when I test another similar method that takes an element as a attribut, place(h1).
Test Class:
HumainImpl<Humain> humain = new HumainImpl<Humain>();
private Humain h1 = new Humain("AAA" , 2200);
private Humain h2 = new Humain("DDD" , 500);
#Test
public void testAddAmount(){
List<Humain> testing = new ArrayList<Humain>();
testing.add(h1);
testing.add(h2);
humain.placer(testing); //placer(testing) is the method placer with a list parameter
}
HumainImpl:
private boolean existe(T elt){
boolean result = false;
index = uneListe.indexOf(elt);
if(index >= 0){
result = true;
}
return result;
}
#Override
public boolean placer(T elt) {
boolean result = false;
boolean found = false;
int i = -1;
int nbExemple;
if(elt != null){
boolean existe = existe(elt);
nbExemple = nbExemplaires(elt);
if(uneListe.size() > 0 && existe){
while(!found && i < uneListe.size()){
i++;
if(elt.equals(uneListe.get(i))){
found = true;
}
}
T element = uneListe.get(i);
nbExemple = element.retournerNbExemplaires();
elt.additionner(nbExemple);
result = false;
} else if(!existe && nbExemple >= 1 &&
uneListe.size() < nbPlacesMaximum) {
uneListe.add(elt);
result = true;
}
}
return result;
}
#Override
public void placer(List<T> liste) {
iterateur(liste);
while(liste != null && liste.size() > 0 && itList.hasNext()){
placer(itList.next());
}
}
In the class Humain:
#Override
public void additionner(int nbExemplaires) {
this.nbExemplaires += nbExemplaires;
}
#Override
public int retournerNbExemplaires() {
return nbExemplaires;
}
my first time reaching out for help to solve a question, not sure what is causing the issue. I have these two classes I wrote, and the assignment asks me to make a test driver proving that the classes work.
Student Class:
public class Student{
private Course[] courseList;
private static int numCourses;
private final int maxCourses;
public Student(int max){
maxCourses = max;
courseList = new Course[numCourses];
numCourses = 0;
}
// part 1, done
public boolean addCourse(Course newClass){
boolean success = false;
for(int i=0; i<=maxCourses; i++){
if(i == numCourses){
courseList[i] = newClass;
success = true;
numCourses++;
}
}
return success;
}
// part 2, done
public boolean dropCourse(Course oldClass){
boolean success = false;
for(int i=0; i<=courseList.length; i++){
if (courseList[i] == oldClass){
courseList[i] = null;
numCourses--;
success = true;
}
}
return success;
}
// part 3, done.
public int getNumCourses(){
return numCourses;
}
//part 4, done
public boolean isFullTime(){
boolean success = false;
if (numCourses >= 3){
success = true;
}
return success;
}
// part 5, done
public String getClassList(){
String list = "";
for(int i=0;i<=numCourses; i++){
list = courseList[i].getID() + "\t" + courseList[i].getName() + "\n";
}
return list;
}
}
and a Course class:
public class Course{
private String name;
private int id;
private static int nextID = 200000;
public Course(String nameIn)
{
name = nameIn;
id = nextID;
nextID++;
}
public String getName(){
return name;
}
public int getID(){
return id;
}
}
For some reason if I make a test driver even one as simple as:
public class tester{
public static void main(String[] args){
Course one = new Course(Java);
}
}
I receive an error at my parameter saying cannot find symbol
javac tester.java
tester.java:6: error: cannot find symbol
one = new Course(name);
^
symbol: variable name
location: class tester
1 error
I had a much longer test driver but it did not make it past the first few lines as this was the same error just several of the same error.
Thanks for your time.
EDIT: I had put the Student class in twice by mistake, in my short test driver the only class in use is the Course class.
The problem is that because there are not quotes around Java, the compiler takes it as a variable name. Since it is undefined, the compiler cant find it. Use Course one = new Course("Java");