Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,709 members, 7,802,138 topics. Date: Friday, 19 April 2024 at 09:40 AM

Help On Java - Programming (3) - Nairaland

Nairaland Forum / Science/Technology / Programming / Help On Java (9558 Views)

Pls Help Me With Ebooks On Java,d Price For The Book Is High And Am Still A Teen / Anyone With Help On Java App Creation? / [Help Request] Help On Java download and Installation Oo! (2) (3) (4)

(1) (2) (3) (4) (5) (Reply) (Go Down)

Re: Help On Java by Dolemite(f): 1:10am On Apr 27, 2011
Thanks alot, silly me i could KISS you right now lol, it really helped, but I would also need to a remove passenger functionality. . .

EDIT**I modified the code thus;
public class Aircraft
{
private ArrayList<Passengers>pass=new ArrayList<Passengers>();
String name;
int fare;
int weight;
int luggage;
Passengers p1 = new Passengers(name,fare,weight,luggage);
public Aircraft(String i,int f, int w, int l){
name=i;
fare=f;
weight=w;
luggage=l;

}
/** public void addPass(Passengers p){
pass.add(p);
System.out.println(p.toString());
}
*/
public void addPass(String i,int f, int w, int l){
//create an instance of Passengers to be added
int tpw=w*l;
if(tpw>250){
System.out.println("Too much weight"wink;
System.out.println(i+"Cannot be added!"wink;
}else{
pass.add(p1); //add the new created object to the arralist
System.out.println("You have just added"+i); //print out the name of the passanger u just added by getting its name u initialized
}
}
public void checkPass(){
System.out.println(p1.getName());

}

}
I get a 'null' whenever i run the checkPass() method, why is that?
Re: Help On Java by segsalerty(m): 1:35am On Apr 27, 2011
Dolemite:

Thanks alot, silly me i could KISS kiss you right now lol, it really helped, but I would also need to a remove passenger functionality. . .

are u female? if(true) kiss kiss kiss kiss kiss 1 million times coz we have few female java programmers.
Sure, u can remove a Passanger by saying


public void removePassanger(String nameOfPassanger){
int passangerIndexToRemove = 0;
for(Passangers p : pass){
if(p.getName().equals(nameOfPassanger){
pass.remove(passangerIndexToRemove);
}
else{
passangerIndexToRemove++;
}
}
}


including the function above can do it ,
it will remove the passanger from the ArrayList of u provide the passanger name on the list !
Hope this helps ? wink
Re: Help On Java by Dolemite(f): 1:39am On Apr 27, 2011
Edited. . . Thanks
Re: Help On Java by segsalerty(m): 1:43am On Apr 27, 2011
Dolemite:

Edited. . . Thanks
works ? u welcome thou wink
Re: Help On Java by Dolemite(f): 1:46am On Apr 27, 2011
EDIT**I modified the code thus;
public class Aircraft
{
private ArrayList<Passengers>pass=new ArrayList<Passengers>();
String name;
int fare;
int weight;
int luggage;
Passengers p1 = new Passengers(name,fare,weight,luggage);
public Aircraft(String i,int f, int w, int l){
name=i;
fare=f;
weight=w;
luggage=l;

}
/** public void addPass(Passengers p){
pass.add(p);
System.out.println(p.toString());
}
*/
public void addPass(String i,int f, int w, int l){
//create an instance of Passengers to be added
int tpw=w*l;
if(tpw>250){
System.out.println("Too much weight"wink;
System.out.println(i+"Cannot be added!"wink;
}else{
pass.add(p1); //add the new created object to the arralist
System.out.println("You have just added"+i); //print out the name of the passanger u just added by getting its name u initialized
}
}
public void checkPass(){
System.out.println(p1.getName());

}

}
I get a 'null' whenever i run the checkPass() method, why is that?

I haven't tried that one though, the remove command. . .
Re: Help On Java by segsalerty(m): 1:54am On Apr 27, 2011
Dolemite:

EDIT**I modified the code thus;I get a 'null' whenever i run the checkPass() method, why is that?

I haven't tried that one though, the remove command. . .

u got this wrong
use this

public class Aircraft
{
private ArrayList<Passengers>pass=new ArrayList<Passengers>();
String name;
int fare;
int weight;
int luggage;
Passengers p1;
public Aircraft(String i,int f, int w, int l){
name=i;
fare=f;
weight=w;
luggage=l;
p1 = new Passengers(name,fare,weight,luggage);
}
/** public void addPass(Passengers p){
pass.add(p);
System.out.println(p.toString());
}
*/
public void addPass(String i,int f, int w, int l){
int tpw=w*l;
if(tpw>250){
System.out.println("Too much weight"wink;
System.out.println(i+"Cannot be added!"wink;
}else{
pass.add(p1); //add the new created object to the arralist
System.out.println("You have just added"+i); //print out the name of the passanger u just added by getting its name u initialized
}
}
public void checkPass(){
System.out.println(p1.getName());
}

}


sad sad sad sad ur coding logic is kinda poor, are u copying/modifying any code there?
Re: Help On Java by Dolemite(f): 11:56am On Apr 27, 2011
I tried your code, it still doesn't work. . .
Re: Help On Java by segsalerty(m): 3:10pm On Apr 27, 2011
Thats the modules logic my friend, It depends on ur class structure and how u implementing it. Try to fix it, if u cant, zip all ur src codes and send to my mail : segsalerty@yahoo.com
Re: Help On Java by Dolemite(f): 5:02pm On Apr 27, 2011
public class Aircraft
{
private ArrayList<Passengers>pass=new ArrayList<Passengers>();
private int seats;
private String name;
private int fare;
private int weight;
private int luggage;
private int tpw;
private int totalfares;
private Passengers p;
public void addPass(String i,int f, int w, int l){
name=i;
fare=f;
weight=w;
luggage=l;
p= new Passengers(name,fare,weight,luggage);
tpw=w*l;
if(tpw>250){
System.out.println("Too much weight"wink;
System.out.println(i+"Cannot be added!"wink;
}else{
pass.add(p);
totalfares=totalfares+f; //add the new created object to the arralist
System.out.println("You have just added"+i); //print out the name of the passanger u just added by getting its name u initialized
}
}
public void checkPass(){
System.out.println(pass);
}
public int checkTotalfares(){
System.out.println(totalfares);
return totalfares;
}
public void getTpw(){
p.getTpw();

}
public void TotalTpw(){
p.getTotalTpw();
}

}


Module what now? anyway this one works, please have a look and tell me if my logic is faulty. . .
Re: Help On Java by segsalerty(m): 6:34pm On Apr 27, 2011
U did quite well with d Aircraft class desing, its just looking as if u re getting stucked trying to get ur class properties. Well, i get ur logic, will do smth to it when i connect to my PC tonite, coz am on mobile rite nw. Pls, be patient wit me.
****when i said nothing wrong wit the modules i provoded to u earlier, i meant those functions i provided to u will do there job as u required if u use them in the right way in ur class to suit ur need.
Re: Help On Java by segsalerty(m): 10:14pm On Apr 27, 2011

public class Aircraft
{
private ArrayList<Passengers>pass=new ArrayList<Passengers>(); //holdin all passangers
private int seats;
private String name;
private int fare;
private int weight;
private int luggage;
private int tpw;
private int totalfares;
private Passengers p;
public void addPass(String i,int f, int w, int l){
name=i;
fare=f;
weight=w;
luggage=l;
p= new Passengers(name,fare,weight,luggage);
tpw=w*l;
if(tpw>250){
System.out.println("Too much weight"wink;
System.out.println(name +" Cannot be added!"wink;
}else{
pass.add(p); //add the passanger
totalfares=totalfares+fare; //add to the total fares
System.out.println("You have just added "+name);
}
}
public ArrayList<Passengers> getPass(){
System.out.println(pass);
return pass;
}
public int getTotalfares(){
System.out.println(totalfares);
return totalfares;
}
}


this shld help?
Re: Help On Java by Dolemite(f): 10:36pm On Apr 28, 2011
Thanks, good to have a genius like you here, you're the ONLY useful person here on nairaland!! grin
Re: Help On Java by Fayimora(m): 12:13am On Apr 29, 2011
Thanks, good to have a genius like you here, you're the ONLY useful person here on nairaland!!


Hehehe !!!!
Re: Help On Java by segsalerty(m): 12:31am On Apr 29, 2011
Dolemite:

Thanks, good to have a genius like you here, you're the ONLY useful person here on nairaland!! grin

U are so much welcome! u dnt need to thank me again, u already gave the sweet kiss kiss kiss kiss kiss kiss kiss some post back! lolz
And mind u, am not the ONLY useful person here on nairaland ooo ! pls, dnt make my head over-swell oooo! We so much have so many talented people on here. ok?

Fayimora:


Hehehe !!!!

Da Main Boss, whats that suppose to mean ? wink
Re: Help On Java by Fayimora(m): 8:24am On Apr 29, 2011
Just feeling the comment man!
Re: Help On Java by candylips(m): 8:37pm On Apr 29, 2011
segsalerty:


public class Aircraft
{
    private ArrayList<Passengers>pass=new ArrayList<Passengers>(); //holdin all passangers
    private int seats;
    private String name;
    private int fare;
    private int weight;
    private int luggage;
    private int tpw;
    private int totalfares;
    private Passengers p;
   public void addPass(String i,int f, int w, int l){
       name=i;
       fare=f;
       weight=w;
       luggage=l;
       p= new Passengers(name,fare,weight,luggage);
       tpw=w*l;
       if(tpw>250){
           System.out.println("Too much weight"wink;
           System.out.println(name +" Cannot be added!"wink;
        }else{
          pass.add(p); //add the passanger
          totalfares=totalfares+fare; //add to the total fares
          System.out.println("You have just added "+name);
        }
    }
    public ArrayList<Passengers> getPass(){
      System.out.println(pass);
      return pass;     
    }
    public int getTotalfares(){
        System.out.println(totalfares);
        return totalfares;
    }   
}


You can declare p inside the addPass method. no need making it an instance variable
Re: Help On Java by Dolemite(f): 3:07am On May 01, 2011
I tried to use the remove passenger method but i get a 'concurrent modification exception' error at runtime. sad
Re: Help On Java by candylips(m): 11:24am On May 01, 2011
^^
Re: Help On Java by Dolemite(f): 10:47pm On May 01, 2011
Segs where are youuuuuuuuuuuuuuuuuu??
Re: Help On Java by candylips(m): 10:53pm On May 01, 2011
removed
Re: Help On Java by Dolemite(f): 11:05pm On May 01, 2011
Segs has a better knowledge of my code. I appreciate your help. smiley
Re: Help On Java by Fayimora(m): 12:55am On May 02, 2011
Segs has a better knowledge of my code. I appreciate your help.

Lmao, wow! but thats a bit harsh on those that wanted to help and I would have fallen into that cause i wanted to help. Would get Segs for you now,
Re: Help On Java by Dolemite(f): 1:05am On May 02, 2011
Lol, no no he's been helping me with the code, all help is welcomed please. . . grin
Re: Help On Java by Fayimora(m): 1:12am On May 02, 2011
Alright, so what do you have and what do you want to do?
Re: Help On Java by segsalerty(m): 2:39am On May 02, 2011
candylips:

removed
u wanna remove me ?  cry cry cry cry or  think i ran away ? shocked shocked shocked

Dolemite:

Segs has a better knowledge of my code. I appreciate your help.  smiley

U really love to make my head swell the more, My Head will just Burst one day and there will be No Segsalerty to call again after my head is gone off my body,  there are other dudes that are very brilliant here,  nobody is [b]Perfect[/b]est grin

Fayimora:

Lmao, wow! but thats a bit harsh on those that wanted to help and I would have fallen into that cause i wanted to help. Would get Segs for you now,

Lol Dude, she really didnt means to, she is just kinda used to giving me those kisses kiss , u know ? but,  this time around, its your turn to have some sweet kisses on your lips to show alot of Appreciation after u've helped her! Thanks in advance wink wink wink wink wink

Dolemite:

Lol, no no he's been helping me with the code, all help is welcomed  please. . . grin

Good ! u just spoken well smiley smiley So, post the problem code so that he can help u,  what if i travelled nko?
Re: Help On Java by Dolemite(f): 8:31am On May 02, 2011
public class Aircraft
{
private ArrayList<Passengers>pass=new ArrayList<Passengers>(); //holdin all passangers
private int seats;
private String name;
private int fare;
private int weight;
private int luggage;
private int tpw;
private int totalfares;
private Passengers p;
public void addPass(String i,int f, int w, int l){
name=i;
fare=f;
weight=w;
luggage=l;
p= new Passengers(name,fare,weight,luggage);
tpw=w*l;
if(tpw>250){
System.out.println("Too much weight"wink;
System.out.println(name +" Cannot be added!"wink;
}else{
pass.add(p); //add the passanger
totalfares=totalfares+fare; //add to the total fares
System.out.println("You have just added "+name);
}
}
public ArrayList<Passengers> getPass(){
System.out.println(pass);
return pass;
}
public int getTotalfares(){
System.out.println(totalfares);
return totalfares;
}
}
How can I implement a remove passenger method in this code that searches for the passenger by name and removes the whole object of the passenger. . .
Re: Help On Java by csharpjava(m): 10:41am On May 02, 2011
Dolemite
How can I implement a remove passenger method in this code that searches for the passenger by name and removes the whole object of the passenger. . .
Try this method:

[s]public void RemovePassenger(ArrayList<Passengers> pass){
int index = pass.indexOf(name);
if (index != -1)
{
pass.remove(name);
}
else
{
System.out.println("passanger not in list"wink;
}

return;
} [/s]

Sorry these should be done with an Iterator
Re: Help On Java by Dolemite(f): 11:16am On May 02, 2011
Thanks alot, but how do i test for this? and does it take off the whole object from the list?

eg. objectofaircraft.RemovePassenger("John"wink; // won't compile.
Re: Help On Java by 2muchlogic(m): 11:50am On May 02, 2011
csharpjava:

DolemiteTry this method:

public void RemovePassenger(ArrayList<Passengers> pass){
        int index = pass.indexOf(name);
        if (index != -1)
        {
        pass.remove(name);
        }
        else
        {
        System.out.println("passanger not in list"wink;
        }
       
        return;
    }     

Its my turn to have some sweet kisses too grin


The code is incorrect, you are passing an ArrayList of Passenger objects when you should have sent the Passenger object, then you are checking for the "name" (which is a Passenger Object Attribute - String Object) from a data structure that contains Passenger Objects 
Re: Help On Java by Dolemite(f): 12:04pm On May 02, 2011
2much what do you suggest I do?
Re: Help On Java by 2muchlogic(m): 12:46pm On May 02, 2011
Dolemite:

2much what do you suggest I do?


The solution: 

The Passenger Object is incomplete, by the look of things each Passenger has 4 variables:  name,fare,weight,luggage.  None of these attributes are unique.

In the REAL WORLD each passenger is UNIQUE and the details will be stored in a database; hence each passenger will be identified by an exclusive passenger NUMBER.  So your class would be something like:

public class Passenger {
int passengerID;
int seats;
String name;
int luggage;

public Passenger(int seats, String name, int luggage, int passengerID) {
this.passengerID = passengerID;
this.seats = seats;
this.name = name;
this.luggage = luggage;
}
}

Now you can create Passengers with Unique Ids and this is equivalent to imitating a database.  An sql query would be something like:

DELETE FROM passengers
WHERE passengerID = some_passengerID;

so in code we could write:

public void removePassenger(int pId) {

//Iterate through the arrayList and check for the ID

//Once you find the Passenger with the correct ID use the remove(Object) method of the ArrayList
}


********** This problem could be solved easier by using a HashMap  instead of an ArrayList *********
Re: Help On Java by Fayimora(m): 1:22pm On May 02, 2011
You probably want to look up Maps, Thats exactly what you need to give the perfect algorithm. Its also designed to help in problems like this

(1) (2) (3) (4) (5) (Reply)

Female Programmers Stand Up And Be Counted / Nigerian Programmers Can Be Of Great Help In The 2019 General Elections. / Are You A Real Programmer? Can You Code With Notepad?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 59
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.