Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Java in General
Spotting errors
Kamran Khaen
Greenhorn
Posts: 15
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
if anyone has the time im, going to submit this assignment like in 4 5 hours anyone want to read through it and tell me if the can spot any mistakes
STUDENT CLASS:
public class Student { // fields private String name; private int achievementPts; private int age; //1.1 private int idNumber; /** * Creates a Student with a unique id number and a name initialised from * parameter values, * @param id it is the responsibility of the client to ensure that id is a unique number * @param n is the String representation of the Student's name * @param age is the strudent's age */ public Student (String n, int age, int id) { name = n; achievementPts = 0; this.age = age; //1.1 idNumber = id; } /** * returns Student's achievement points as an int * @return value returned is Student achievement points as an int */ public int getAchievementPts () { return achievementPts; } /** * returns Student's age * @return value returned is Student achievement points as an int */ public int getAge () { return age; } /** * allows a Students achievementPts to be decremented * @param pts is the number by which a Student's achievementPts will be incremented */ public void decrementPts (int pts) { achievementPts = achievementPts - pts; } /** * returns whether the player is a junior or not * @return value true if player is a junior */ public boolean isJunior() { return age < 18 ; } /** * returns whether the player is a mature or not * @return value true if player is a mature */ public boolean isMature () { return age > 21; } //1.2 public int getId() { return idNumber; } //1.3 public void incrementPoints(int p) { achievementPts = achievementPts + p; } //1.4 public boolean isSenior() { if(age >= 18 && age <= 21) { return true; } else { return false; } } //1.6 public String toString() { String StudentInfo = "Student ID: " + idNumber + "Name: " + name + "Points: " + achievementPts + "Age: " + age + "Type: " + getType(); return StudentInfo; } public String getType() { if(isJunior()) return "Junior"; if(isSenior()) return "Senior"; return "Mature"; } }
MEETING CLASS import java.util.*; import java.util.ArrayList; /** * Stores information about Race meetings including details of * the team * * @author * @version (a version number or a date) */ public class Meeting { // declare fields private String code; private Date raceDate; private double cost; private int points; private double totalCosts = 0; //2.1 private String venue; private int maxStudents; private ArrayList<Student> team; public Meeting (String code, String Location, int maxStudents, int points, double cost) { this.code = code; venue = Location; maxStudents = maxStudents; points = points; cost = cost; raceDate = new Date (1,1,2000); team = new ArrayList < Student > (); } /** * returns the meeting code * @return the meeting code */ public String getCode() { return code; } /** returns the meeting attendance points * @return the meeting attendance points */ public int getPoints() { return points; } /** returns the total costs collected for the meeting * @return the total costs collected for the meeting */ public double getTotalCosts() { return totalCosts; // all the work for this is done in signUp() } /** Withdraws one Student from this race meeting * - see specification for details */ public void withdraw(Student m) { if (team.contains(m)) // optional { team.remove(m); m.decrementPts(points); } } /** Returns true if a student is signed up for the meeting * @return true if a student is signed up for the meeting */ public boolean isSignedUp(Student m) { return team.contains(m); // return a suitable value } //2.3 public void setRaceDate(int d,int m,int y) { raceDate.setDate(d,m,y); } // 2.3 public String getVenue() { return venue; } public double getCost() { return cost; } public Date getRaceDate() { return raceDate; } public String getMeetingDetails() { String details = String.format("%s at %s on %s Costs £%.2f\n", getCode(), getVenue(), getRaceDate().getAsString(), getCost()); details += String.format("Maximum Students: %d, Attendance points: %d", maxStudents,points); return details; } public void signUp(Student std) { if(team.size() < maxStudents) { this.team.add(std); if(std.isJunior()) this.totalCosts = (cost/2.0); if(std.isSenior()) this.totalCosts = 10; if(std.isMature()) this.totalCosts = cost; std.incrementPoints(points); } } public boolean hasPlaces() { return team.size() < maxStudents; } public String getStudents() { String details = ""; for (Student std : team) { details = std.toString(); } return details; } public String toString() { return getMeetingDetails() + getStudents(); } }
SOCIETY CLASS
i
mport java.util.*; public class Society { private String location; private ArrayList<Meeting> allMeetings = new ArrayList<Meeting>(); private ArrayList<Student> allStudents = new ArrayList<Student>(); /** A constructor for the Society */ public Society(String loc) { this.location = loc; } /** Adds a race meeting to the Society */ public void addMeeting(Meeting ral) { if (!allMeetings.contains(ral)) { allMeetings.add(ral); } else {System.out.println("Meeting already exists");} } /** Allows a Student to withdraw from a race meeting */ public void withdrawFromMeeting(Student m, Meeting r) // note: points handled in Meeting { if (r.isSignedUp(m)) { r.withdraw(m); } else {System.out.println("Student not signed up for this RaceMeeting");} } /** Displays all race meetings in the Terminal Window */ public void printAllMeetings() { for (int index = 0; index <allMeetings.size(); index++ ) { Meeting r = allMeetings.get(index); System.out.println(r.toString()); } } /** returns true if the code given as by the parameter value * is the code for a Meeting at the Society, false otherwise * @return true if the code given as by the parameter value * is the code for a Meeting at the Society, false otherwise */ public boolean isMeeting(String t) { for (Meeting temp : allMeetings) { if (t.equals(temp.getCode())) { return true; } } return false; } public void addStudent(Student std) { if(!allStudents.contains(std)) allStudents.add(std); } public void signUpForMeeting(Student std, Meeting m) { if(m.hasPlaces()) { m.signUp(std); System.out.println("Sign up successful"); } else { System.out.println("Meeting is full - You cannot sign up"); } } public void printAllStudents() { for(Student std : allStudents) { System.out.println(std); } } public boolean isStudent(int id) { for(Student std : allStudents) { if(std.getId() == id) return true; } return false; } public void printAllInfo() { System.out.println("Society Location: " + location); System.out.println("Meeting details"); for(Meeting m : allMeetings) { System.out.println(m.toString()); } for(Student s : allStudents) { System.out.println(s); } } public void printJuniors() { for(Student s : allStudents) if(s.isJunior()) System.out.println(s); } public void addExtraPoints(Student std, int p) { std.incrementPoints(p); } public void printPoorSalors(int p) { for( Student s : allStudents) { if(s.getAchievementPts() < p) System.out.println(s); } } }
DATE CLASS
/** public class Date { /** Fields of a Date - just the day, month and year*/ private int day; private int month; private int year; /** * Parameterless constructor for objects of class Date * Date set to 1/1/2000 */ public Date() { day = 1; month = 1; year = 2000; } /** * Constructor for objects of class Date * @param d - the day part of the date (1 - 31, depending on the month). * @param m - the month part of the date (1 - 12). * @param y - the year part of the date.*/ public Date(int d, int m, int y) { day = d; month = m; year = y; } /** * method to set the Date * @param d - the day part of the date (1 - 31, depending on the month). * @param m - the month part of the date (1 - 12). * @param y - the year part of the date. * Note: No error checking in this version ! */ public void setDate(int d, int m, int y) { day = d; month = m; year = y; } /** * @return the date as a String, format "09/11/2002" */ public String getAsString () { return as2Digits(day) + "/" + as2Digits(month) + "/" + year; } /** Internal method to add a leading zero if necessary. */ private String as2Digits (int i) { if (i <10) {return "0" + i;} else {return "" + i;} } }
carina caoor
Ranch Hand
Posts: 300
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
No one has got this much time to go through the code.
Rob Spoor
Sheriff
Posts: 22849
132
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Rename Date's "getAsString" method to "toString", and add more Javadoc comments.
if(age >= 18 && age <= 21) { return true; } else { return false; }
Change this into simpler form:
return age >= 18 && age <= 21;
It is possible to for a student to sign up twice for the same meeting; shouldn't you prevent this?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Campbell Ritchie
Marshal
Posts: 80639
471
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
As well as Rob's comment about signing up the same student twice,
you should
consider throwing an Exception if that happens, or if the number of possible students is exceeded.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
help creating mutator
help creating mutator
New to Java and wanting to learn
Date Class Problems
DateRec
free 1 hour java lesson
More...