Tuesday 15 September 2015

Struggling with objects, program does not work as expected(java) -



Struggling with objects, program does not work as expected(java) -

so have cyclingmanager game, , want show list of riders names, , want show abilities when user picks rider. programme compiles , runs nicely, problem in riders() method.it not print out c1, first rider. in advance.

import java.util.arraylist; import java.util.list; import java.util.scanner; public class cyclingmanager2 { public static void main(string[] args) { //menyvalgene menu m = new menu(); m.choice(); } } class menu { scanner in = new scanner(system.in); cyclist cy = new cyclist(); //choices public void choice() { int selection = -1; while (choice != 0) { system.out.println("choose something: "); system.out.println("-0 exit program" + "\n-pressing 1 open database menu"); selection = in.nextint(); switch(choice) { case 0: selection = 0; break; case 1: database(); break; default: system.out.println("you have take either 0 or 1"); break; } } } public void database() { system.out.println("welcome database \nthese options:\n0 = menu\n1: riders"); int dbchoice = -1; while (dbchoice != 0) { system.out.println(); dbchoice = in.nextint(); switch(dbchoice) { case 0: dbchoice = 0; break; case 1: cy.riders(); system.out.println("press 0 going menu\npress 1 showing riders");break; default: system.out.println("choose either 0 or 1"); break; } } } } class cyclist { list<cyclist> cyclists = new arraylist<>(); private string name; private int mountain; private int timetrial; private int sprint; private int age; cyclist c1 = null; public void setname(string name) { this.name = name; } public string getname() { homecoming name; } public void setmountain(int mountain) { this.mountain = mountain; } public int getmountain() { homecoming mountain; } public void settimetrial(int timetrial) { this.timetrial = timetrial; } public int gettimetrial() { homecoming timetrial; } public void setsprint(int sprint) { this.sprint = sprint; } public int getsprint() { homecoming sprint; } public void setage(int age) { this.age = age; } public int getage() { homecoming age; } public void riders() { abilities(); (int = 0; < cyclists.size(); i++){ system.out.print(((cyclist) cyclists).getname()); } } public void abilities() { //pardilla created c1 = new cyclist(); c1.setname("sergio pardilla"); c1.setmountain(75); c1.settimetrial(60); c1.setsprint(60); c1.setage(30); /*system.out.println(c1.getname() + "'s abilities:"); system.out.println("mountain - " + c1.getmountain()); system.out.println("timetrial - " + c1.gettimetrial()); system.out.println("sprint - " + c1.getsprint()); system.out.println("age - " +c1.getage()); cyclists.add(c1); //adds pardilla cyclists arraylist*/ } }

i think want more like:

public void riders() { abilities(); (int = 0; < cyclists.size(); i++){ system.out.print(cyclists.get(i).getname()); } }

another thing, i'm not sure want list<cyclist> cyclists = new arraylist<>(); part of cyclist class.

java object-reference

No comments:

Post a Comment