wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Struggling to code this. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Struggling to code this." Watch "Struggling to code this." New topic
Author

Struggling to code this.

stefan balling
Greenhorn

Joined: Nov 28, 2012
Posts: 7
Hi all!

First time poster so I apologise if I make any nooby mistakes in advance!

I'm currently studying CS and i'm in my second year at Glasgow uni learning Java. Now I'll admit i'm really struggling with it this year.

I have some course work which I'm working through and I have an idea how to code it but I'm struggling with how.

The program is to check the eligibility of students available for lab times. Two text files (course.txt which contains course name then subsequent lines are the lab group and times) and (students.txt which contains lines starting with the student ID then all the times they are committed to).

The code should print out if the student is eligible or not and if so give him an appropriate lab. Need these classes, Course, Student, Time, TimeSlot and check student. Check student is written and does the eligibility check for us and Time is also written for us and contains methods to make using time easier.

So, my idea was to run through the course file first and take the course name off and pass the remain info into the timeslot class and play around with it there. Then run through the student file and take the student ID into the student class and pass the rest again into timeslot. The problem is I dont know how to seperate the two files when I pass them into the timeslot. I wanted to add a boolean value to determine which one is being passed in but I was told I can't do that. Can anyone give me some guidance? I don't want answers just a step in the right direction!

I feel I didn't explain this well so im sorry if this is really long.

The below is the course work sheet.

Thanks everyone for any help!

Course worksheet wrote:
The purpose of the program is to determine students’ eligibility and availability for lab groups for a given course. The program is to read data from two input files, one containing details of a course and the other details of students.

The Course file

The first line of the file course.txt contains the course name, which may contain embedded spaces, for example

Java Programming 2

Each subsequent line of the file contains information on a laboratory group associated with the course. A typical line has the form

H Mon 13:00 15:00

where the first item is the lab group label (a single character), and the other three items give the time slot for that lab group.

The student file

Each line of the file students.txt contains a student number followed by a sequence of time slots, representing the existing commitments of the student, for example

0903612 Mon 12:00 14:00 Tue 14:00 16:00 Wed 09:00 11:00 Wed 14:00 16:00 Thur 12:00 13:00 Fri 10:00 12:00

(Note that this represents a single line of the file.)

The program

The program is to read the data from the two files and write to standard output the name of the course, followed by one line for each student. This line should contain the student number, an indication of whether the student is eligible for the course, and if so a list of the lab groups for which s/he is available. A student is eligible for the course if at least one of the lab group time slots does not overlap any of his/her commitments, and is available for each of the lab groups with a scheduled time slot that does not overlap any of those commitments1. So the first three lines of output might be:

Java Programming 2
0903612 ineligible
0951411 eligible A C F

You are given a complete class CheckStudents containing a main program that is designed to solve this problem. However, a number of other classes are required to complete the solution.

Task 1. You are given a complete class Time to represent a time in hours and minutes, and a skeleton class TimeSlot to represent a time slot consisting of a day, a start time and a finish time. You are to complete the implementation of this TimeSlot class.

In the TimeSlot class, you should provide a full set of constructors, accessors and mutators, together with equals and toString methods, and any other methods that may be appropriate for the context. In the other classes, you need give only methods that are actually used in your program.

Task 2. You are given a skeleton of a class LabGroup to represent a lab group. You are to complete the implementation of this class.

Task 3. You are given a skeleton of a class Course to represent a course, which involves an unknown number of lab groups. You are to complete the implementation of this class. It is suggested that the class should use an ArrayList to hold the lab groups for the course.

Task 4. You are given a skeleton of a class Student to represent a student, to include information on the student’s commitments and other attributes relevant to the context. You are to complete the implementation of this class.

Notes
1. The program assumes that the input files are in exactly the right format.

2. Each class is to include a constructor that creates an object by reading from a scanner, tailored for the context. This will be the only constructor that is needed in the LabGroup, Course and Student classes. The given Time class has such a constructor2.

3. You should not make any changes to the classes Time or CheckStudents.

4. Some of the methods required in the classes that you have to implement are immediately apparent from inspection of the main program. Others you will have to discover for yourself.

5. You may assume that the days are represented as Mon, Tue, Wed, Thur and Fri (no labs take place at the weekend!)

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1795
    
    7

Hi, and welcome to the Ranch!
I've take the liberty to add quote-tags to the section of your post to make it clear that this is the actual worksheet text.
stefan balling wrote:The problem is I dont know how to seperate the two files when I pass them into the timeslot. I wanted to add a boolean value to determine which one is being passed in but I was told I can't do that.

I don't quite follow what you mean by this, could you elaborate?
Maybe you could describe to us in pseudocode the steps you think the program should take?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome again.
If you were doing that by hand, one way would be to cut out pieces of paper representing the tutorials, and move them around. You could have coloured beads for the students. Try playing around with that sort of thing for half an hour. See whether that will inspire you to think of a few names of classes to put in your app.
stefan balling
Greenhorn

Joined: Nov 28, 2012
Posts: 7
Jelle Klap wrote:Hi, and welcome to the Ranch!
I've take the liberty to add quote-tags to the section of your post to make it clear that this is the actual worksheet text.
stefan balling wrote:The problem is I dont know how to seperate the two files when I pass them into the timeslot. I wanted to add a boolean value to determine which one is being passed in but I was told I can't do that.

I don't quite follow what you mean by this, could you elaborate?
Maybe you could describe to us in pseudocode the steps you think the program should take?




Thanks for the quick reply guys!

Sorry I find it difficult to convey my thoughts on to paper (or computer).

Basically:

1) Read file course.txt into the course class.
2) Take the first part (course name) and store it into course, pass the remaining line down to the lab group class.
3) In the lab group class take the first part again (Lab name A-H) and store that and pass the rest of the line down to timeslot.
4) In timeslot run through the remaining file and store the day, start time and finish time accordingly.
5) Somehow pass the timeslot class back up for the checkstudent class to use?

1) Read the student.txt into the student class.
2) Take the first part of the line (student ID) and store it in the student class, pass the remaining line down to the timeslot class.
3) Repeat 4 in other part.
4) Again somehow pass this back up to the checkstudent class.
5) Check the eligibility and print?

Again I’m not particularly good at this but this is how I thought I could solve this problem.


Thanks.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8052
    
  22

stefan balling wrote:So, my idea was to run through the course file first and take the course name off and pass the remain info into the timeslot class and play around with it there. Then run through the student file and take the student ID into the student class and pass the rest again into timeslot. The problem is I dont know how to seperate the two files when I pass them into the timeslot. I wanted to add a boolean value to determine which one is being passed in but I was told I can't do that. Can anyone give me some guidance? I don't want answers just a step in the right direction!

OK, well firstly, I'd follow Campbell's advice and play around before you start ANY coding. And don't forget to write out your findings in English.

Secondly, I'd split the problem into two major phases:
1. Load the data.
2. Validate it.
In the first phase, don't worry about whether things are correct or not, simply convert all those 'orrible Strings into proper Java objects.

Third, I'd probably add a Day class (or, probably even better, an enum) to the ones you've already got.

Basically, Strings make bad substitutes for other types, so you want to convert as many of them as you can as fast as you can.

However, I do have a question:
The course file contains a LabGroup AND a TimeSlot, but no indication as to whether the two are related or not.
For example: is LabGroup 'H' always associated with the TimeSlot 'Mon 13:00 15:00' - with or without the 'Mon' - or is it simply the designation for a lab room which could be used with any (presumably non-overlapping) TimeSlot?

My suspicion is the latter, but it's something that would be worth finding out before you get too far down the road.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2415
    
  28

I would suggest that you break down the problem into 3 parts a) Reading the student.txt file and loading it into a data structure in memory b) Reading the course. txt file and loading in into a data structure in memory c) merging the 2 inputs to get the output that you need. These are your 3 high level modules.

Next start thinking about what those input and output data structures will look like and how you would merge them. This will guide you towards defining the interface between the modules

Once you have the interfaces designed, work on one module at a time, test them independently, then hook them up and test them all together

As a general guideline, programming is an exercise in breaking down problems into manageable pieces. Don't try to solve the entire problem at once. At any time, a problem starts to overwhelm you, start thinking about how you can break it down into independent manageable pieces. Any problem is solvable as long as you break it down.

stefan balling
Greenhorn

Joined: Nov 28, 2012
Posts: 7
Winston Gutkowski wrote:
stefan balling wrote:So, my idea was to run through the course file first and take the course name off and pass the remain info into the timeslot class and play around with it there. Then run through the student file and take the student ID into the student class and pass the rest again into timeslot. The problem is I dont know how to seperate the two files when I pass them into the timeslot. I wanted to add a boolean value to determine which one is being passed in but I was told I can't do that. Can anyone give me some guidance? I don't want answers just a step in the right direction!

OK, well firstly, I'd follow Campbell's advice and play around before you start ANY coding. And don't forget to write out your findings in English.

Secondly, I'd split the problem into two major phases:
1. Load the data.
2. Validate it.
In the first phase, don't worry about whether things are correct or not, simply convert all those 'orrible Strings into proper Java objects.

Third, I'd probably add a Day class (or, probably even better, an enum) to the ones you've already got.

Basically, Strings make bad substitutes for other types, so you want to convert as many of them as you can as fast as you can.

However, I do have a question:
The course file contains a LabGroup AND a TimeSlot, but no indication as to whether the two are related or not.
For example: is LabGroup 'H' always associated with the TimeSlot 'Mon 13:00 15:00' - with or without the 'Mon' - or is it simply the designation for a lab room which could be used with any (presumably non-overlapping) TimeSlot?

My suspicion is the latter, but it's something that would be worth finding out before you get too far down the road.

Winston



Hi Winston,

Thank you for your response. I haven’t coded yet as I’m not sure how to solve the problem. I cannot add another class unfortunately, I have to use what I’m given. I’ll need to read up on enums as I’m not too sure about them. The problem I can see is each class has to have a constructor using a scanner. Now I’m not sure how to implement that through each class. For example if I go through the file and take the student ID then I want the timeslot class to read in the info after the ID if that makes sense?

I’m also not sure if when I load the remaining data into the timeslot class from both files, they have a different format so I’m not sure how can I load each file through the timeslot class. Does that make sense?

Also to your question, you are right it is the latter, different course may vary to the example given.

Thank you again.
stefan balling
Greenhorn

Joined: Nov 28, 2012
Posts: 7
Jayesh A Lalwani wrote:I would suggest that you break down the problem into 3 parts a) Reading the student.txt file and loading it into a data structure in memory b) Reading the course. txt file and loading in into a data structure in memory c) merging the 2 inputs to get the output that you need. These are your 3 high level modules.

Next start thinking about what those input and output data structures will look like and how you would merge them. This will guide you towards defining the interface between the modules

Once you have the interfaces designed, work on one module at a time, test them independently, then hook them up and test them all together

As a general guideline, programming is an exercise in breaking down problems into manageable pieces. Don't try to solve the entire problem at once. At any time, a problem starts to overwhelm you, start thinking about how you can break it down into independent manageable pieces. Any problem is solvable as long as you break it down.




Thank you for this, it's very helpful!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8052
    
  22

stefan balling wrote:Thank you for your response. I haven’t coded yet as I’m not sure how to solve the problem. I cannot add another class unfortunately, I have to use what I’m given.

Really? I don't see it stated anywhere that you can't add another class if you see fit; that's what design is all about. Indeed, Note 5 sounds like it might actually be hinting at one.

I’ll need to read up on enums as I’m not too sure about them.

Good idea. They're very useful, and extremely easy to create. In fact, yours could be as simple as:
public enum Day { Mon, Tue, Wed, Thu, Fri; }
but I suspect you'll also want to add a method that converts from a String.


The problem I can see is each class has to have a constructor using a scanner. Now I’m not sure how to implement that through each class. For example if I go through the file and take the student ID then I want the timeslot class to read in the info after the ID if that makes sense?

Sure, but I don't think anyone's telling you how you have to implement them. Personally, I'd be tempted to have my TimeSlot class constructor assume that the Scanner it receives is already positioned at the start of a TimeSlot String, and throw an Exception if it isn't. Same with LabGroup.

And just on that point: Did you know that you can create a Scanner for a String as well as a File?

I'd also be tempted to make both of those classes' Scanner constructor package-private so they can't be called by just anyone.

HIH

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You can find out about enums here. But if you ever find the version of that link which went with Java5, it makes better reading.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struggling to code this.