Results 1 to 20 of 46
- 09-02-2009, 06:00 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
New to JAVA and need some help getting a started
Ok, so I'm new to JAVA trying to watch tutorials and learn on my own but I've come to a cross road right in the beginning and really don't know where to start with this program.
This is what I have from my book:
I need to design and implement the class Day (ok not a problem)
The class should store the day like Sun for Sunday and this is what I need the program to do.
set the day
print the day
return the day
return the next day
return the previous day
and some other things that will fall into place once I get this bad boy rolling, getting it rolling is the problem I'm having.
Now I've looked all over the net about the class Day and I think what I found confused me even farther than I was before. Any help is much appreciated and looking forward to learning JAVA.
Thanks in advance.
-
Don't search the net for this as it will only confuse you (as you're finding out), but rather read the chapter in your book, and given the info taught there, try to create this on your own first, and then please come back and post your code if it doesn't work.
- 09-02-2009, 06:17 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Yeah, will do will have some code up tommorow for this and where I'm stuck. Thanks.
- 09-02-2009, 02:10 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
This is what I currently have. The problem with the book is it does nothing with days etc in the first chapter. Its all about the clock and getting different output with certain situations of using the clock, then goes straight to this.
Now where I'm stuck is getting the day set, printing the day, returning the day, returning the next day etc. I've tried a bunch of different things and I get a ton of errors. What my question is, do I need to use different set/get contructors to get it started?Java Code:import java.util.*; public class Day { static Scanner console = new Scanner(System.in); public static void main(String args[]) { String currentday; System.out.println("Enter the day of the week in 3 letter abbreviations IE. Sun = Sunday, Mon = Monday: "); currentday = console.next(); } }
Something on the lines of:
Think I'm overthinking this and confusing myself.Java Code:public static int Sunday; public static int Monday; public static int Tuesday; public static int Wednesday; public static int Thursday; public static int Friday; public static int Saturday; //then have a constructor to get the day? public class(){
- 09-02-2009, 02:30 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 13
- Rep Power
- 0
The first thing you should do is think about the design of the class.
By that I mean what you need to store in the class and what you need the class to do.
Then you will have a list of fields that your class needs and also a list of methods you need to create for your class.
At this point you can create your class, with some methods that are empty and then implement the methods 1 at a time.
You can then use the main method to call each of the methods to ensure they are functioning correctly.
- 09-02-2009, 03:03 PM #6
You don't need different constructors.
For example, lets say you wanted to tell a Day object what day of the week it was during creation. You could do this several ways. One way based on your code is to assign numbers to each day:
then in the constructor you could just assign:Java Code:public int dayOfTheWeek;
then for lookup, you could use a switch/case statement:Java Code:public Day(int day){ dayOfTheWeek = day; }
A more elegant way to do it would be to use enumerated types. Look at this link: http://java.sun.com/docs/books/tutor...vaOO/enum.htmlJava Code:public String getDay(){ switch(dayOfTheWeek){ case 1: return "Monday"; ... } }
Good Luck!Last edited by quad64bit; 09-02-2009 at 03:09 PM.
-
I have to agree with JavaVideos here. The first thing you need to decide is this: is this going to be a true OOP class that you can use to create objects from, that other classes can use, or do you want to create just a trivial class that does nothing more than hold a main method?
My guess given your instructions that you need to "design and implement the class Day ... The class should store the day like Sun for Sunday ...", is that this should be a true OOP class.
While I'd use enums here to represent the days of the week, my guess is that at this stage you'll want to use an constant array of Strings instead. But why not play around with this, throw some code on the wall and see what sticks and then post your next attempt here.
Best of luck.
- 09-02-2009, 05:12 PM #8
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Thanks for the responses. Will be plugging at this and have some code up later today, hopefully it will all start to fall into place.
- 09-02-2009, 09:48 PM #9
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Ok, I've been playing with this quite a bit today and decided to use some source code from the book that is dealing with a clock and setting the hr, min, sec etc.
I'm having an issue in the program and it has to do with the first constructor, not sure if the other constructors are right either but I'm having a real issue understanding the first constructor so heres my code.
Hopefully someone here can steer me in the right direction.Java Code:public class Day { private int currentday; //stores day private int nextday; //stores next day private int previousday; //stores previous day public Day(int currentday) { setDay(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) } //Default constructor //Postcondition; Mon = 1; Tue = 2; .. etc. public Day() { setDay(1, 2, 3, 4, 5, 6, 7); } //Method to return the day public int getDay() { return currentday; } //Method to print the day public void printDay() { if(currentday < 8) System.out.println("Today is " + currentday); } //Method to decrement the day by one public void decrementDay() { previousday = currentday--; System.out.println("Yesterday was " + previousday); } //Method to increment the day by one public void incrementDay() { nextday = currentday++; System.out.println("Tomorrow is " + nextday); } }
I'm thinking I need more variables declared but not sure where to start declaring.Last edited by SMHouston; 09-02-2009 at 10:00 PM.
- 09-02-2009, 10:01 PM #10
Member
- Join Date
- Sep 2009
- Posts
- 13
- Rep Power
- 0
You are definitely making progress now, I can see a few issues, most are minor that will fall in to place when you get the class running.
But the one to address now is, as you point out, the constructors.
In there you are calling a method, setDay, but that is not defined in your class anywhere.
So that needs to be added, however I am not sure what you are trying to archive with that method, so perhaps you can explain what it is mean't to do.
Often by explaining what you are trying to archive you will help yourself find the answer to your problems without too much help from others.
- 09-02-2009, 10:06 PM #11
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Ok, what I am trying to do is get user input by asking what day it is and having the user enter a value 1 for mon, 2 for tues etc.
Then have the program set that day, find the previous day, and the next day and print them out.
For example if the user enters 1 it would print:
Today is Monday
Yesterday was Sunday
Tomorrow is Tuesday
I also need to calculate and reurn the day by adding certain days to the current day. Like if the currentday was Monday and they added 13 it would return Sunday and so on, but I want to get the first part working first.
I know my main issues lie in the first two constructors and I'm completely lost on what I should be doing here as far as setting them right. Hope that makes sense.
Not sure what you mean by the setDay not being defined, the program I am using for reference uses setTime just like that, however its much easier since they are using hr, min, sec not 7 days. I can see if i was using 3 days I could set the defaults but the 7 days is throwing me off.
I actually forgot a couple constructors to return the previous and nextday values that I've added but they are the same as returning the current day, and added an error message if they input 8 or above for numbers that are invalid.Last edited by SMHouston; 09-02-2009 at 10:30 PM.
- 09-02-2009, 10:49 PM #12
Member
- Join Date
- Sep 2009
- Posts
- 13
- Rep Power
- 0
So, I think you are trying to store the Strings Monday, Tuesday etc and then be able to reference them via the numbers 1 - 7.
I would read up about arrays and see if you can use them to do that.
Your constructor takes a parameter called currentday, that is not be in used yet, think about what you need to do with that parameter, you want to keep it somewhere so it can be used by the other methods.
Hope this helps.
-
Just remember that arrays are "zero-based", meaning that the first element in the array is always at index 0, the next at index 1, next at index 2, etc...
So while you can use one through seven as the possible int values for currentDay, you'll always have to subtract the this number by one if you want to use it as an index for the String array.
If you want to avoid this step, then use the numbers zero through six as your possible int values to be held by the currentDay variable.
- 09-02-2009, 11:38 PM #14
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Ok, I'm a bit confused as to where you guys want me to head.
I think the problem in my first two constructors is trying to set currentday to 7 different values, and having the default be 1-7.
So would something like this work without getting into arrays or is it a must for this that I need to do arrays.
I still get a a fatal error and can't even run the program. I'm sorry if I'm sounding dumb but I'm at a complete loss here, think I'm over thinking the situation and just trying to confuse myself even more than I was earlier.Java Code:public class Day { private int currentday; //stores day private int nextday; //stores next day private int previousday; //stores previous day private String Mon; private String Tue; private String Wed; private String Thu; private String Fri; private String Sat; private String Sun; //Constructor with parameters, to set the day //The day is set according to the parameters //Postcondition: ???? public Day(String Monday, String Tuesday, String Wednesday, String Thursday, String Friday, String Saturday, String Sunday) { setDay(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); } //Default constructor //Postcondition; Mon = 1; Tue = 2; .. etc. public Day(int currentday) { setDay(1, 2, 3, 4, 5, 6, 7); }
- 09-02-2009, 11:46 PM #15
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
- 09-03-2009, 12:01 AM #16
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Ok, read up on arrays and I think I'm doing this right. However, the first two constructors are gonna throw me off and that means I'll probably need to change all my constructors based on using arrays now?
I have:
These are the first two constructors going down the list. I'm lost at this point.Java Code:public class Day { private String[] daysoftheweek = new String[6]; private String currentday;//stores day private String nextday; //stores next day private String previousday; //stores previous day daysoftheweek[0] = "Monday"; daysoftheweek[1] = "Tuesday"; daysoftheweek[2] = "Wednesday"; daysoftheweek[3] = "Thursday"; daysoftheweek[4] = "Friday"; daysoftheweek[5] = "Saturday"; daysoftheweek[6] = "Sunday"; //Constructor with parameters, to set the day //The day is set according to the parameters //Postcondition: ???? public Day(String currentday) { setDay(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); } //Default constructor //Postcondition; Mon = 1; Tue = 2; .. etc. public Day() { setDay(1, 2, 3, 4, 5, 6, 7); }
-
A few important rules to remember when programming is this:
- First start out with a skeleton of your class, i.e.,
Java Code:public class MyClass { } - Compile this skeleton and make sure that there are no errors. If you find any, fix them now, and keep compiling until they are fixed.
- Only add a small amount of code at a time.
- Compile the code after each small addition.
- If there are compile errors, do not add any more code. First fix the errors before you move on.
If you don't do this, you may end up with code that contains a gazillion errors. Trust me. I've been there.
- First start out with a skeleton of your class, i.e.,
-
Please note that you can't do what you're trying to do in the location that you're trying to do it. This code:
is in the section of your class where variables and such are declared, but since you're calling methods on a variable that already exists, it won't work, and a compiler error will be thrown. It has to be in a method or constructor to be considered good code.Java Code:daysoftheweek[0] = "Monday"; daysoftheweek[1] = "Tuesday"; daysoftheweek[2] = "Wednesday"; daysoftheweek[3] = "Thursday"; daysoftheweek[4] = "Friday"; daysoftheweek[5] = "Saturday"; daysoftheweek[6] = "Sunday";
Perhaps better would be to use a String array as you are doing, but would create your String at its declaration, like so:
This code can be located where your code above is located as it is considered a variable declaration.Java Code:private String[] daysOfTheWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
I would also have my currentDay variable be an int since it seems to work better with the methods that you'll have to create later:
Now this code here:Java Code:private int currentDay;
just doesn't make much sense to me. I'm not sure just what you're trying to do here but you can't make up methods and hope that they'll work. If you tell us just what this is supposed to do, perhaps we can help set you straight.Java Code:public Day(String currentday) { // this makes no sense setDay(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); }
Another suggestion: nextDay and previousDay shouldn't be variables but they should be methods. In these methods, you'll take the currentDay int and use it to return the prevousDay int or next day int. You will need to add or subtract from the current day int to get the result you want, but be careful. If the result is less than 0 or greater than 6 it won't make sense, and so you'll need to do something to fix this. The most straight-forward fix is to use the modulus operator % to correct this (you can find a lot about this at the Sun tutorials). Another simpler though longer way is to use some if blocks. Either way will work.Last edited by Fubarable; 09-03-2009 at 12:59 AM.
- 09-03-2009, 01:14 AM #19
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Ok, will keep plugging. What got me in that state of mind with setDay is this:
I was trying to reproduce the problem preety much using this source code as a guideline of sorts and figured it could be done like that.Java Code:public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds //Constructor with parameters, to set the time //The time is set according to the parameters //Postcondition: hr = hours; min = minutes; sec = seconds public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } //Default constructor //Postcondition: hr = 0; min = 0; sec = 0 public Clock() { setTime(0, 0, 0); } //Method to set the time //The time is set according to the parameters //Postcondition: hr = hours; min = minutes; sec = seconds public void setTime(int hours, int minutes, int seconds) { if(0 <= hours && hours < 24) hr = hours; else hr = 0; if(0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec = seconds; else sec = 0; } //Method to return the hours //Postcondition: the value of hr is returned public int getHours() { return hr; } //Method to return the minutes //Postcondition: the value of min is returned public int getMinutes() { return min; } //Method to return the seconds //Postcondition: the value of sec is returned public int getSeconds() { return sec; } //Method to print the time //Postcondition: Time is printed in the form hh:mm:ss public void printTime() { if(hr < 10) System.out.print("0"); System.out.print(hr + ":"); if(min < 10) System.out.print("0"); System.out.print(min + ":"); if(sec < 10) System.out.print("0"); System.out.print(sec); } //Method to increment the time by one second //Postcondition: The time is incremented by one second //If the before-increment time is 23:59:59, the time //is reset to 00:00:00 public void incrementSeconds() { sec++; if(sec > 59) { sec = 0; incrementMinutes(); //increment minutes } } //Method to increment the time by one minute //Postcondition: The time is incremented by one minute //If the before-increment time is 23:59:53, the time //is reset to 00:00:53 public void incrementMinutes() { min++; if(min > 59) { min = 0; incrementHours(); //increment hours } } //Method to increment the time by one hour //Postcondition: The time is incremented by one hour //If the before-increment time is 23:45:53, the time //is reset to 00:45:53 public void incrementHours() { hr++; if(hr > 23) hr = 0; }
-
Ah, thanks for posting that. It is getting a little clearer to me. In the code you posted, Your Clock constructors can call the setTime method because this method exists in the class:
But your class doesn't have a setDay method, nor does it really need one.Java Code:public void setTime(int hours, int minutes, int seconds) { //.... }
Again, I would make currentDay an int variable. I would create a default constructor (one without parameters) that sets currentDay to a default value, say 0. I would create a constructor that accepted an int parameter and set currentDay to that parameter's value. Later on when you learn about exceptions, you'll know what to do if the int passed to the method doesn't make sense (is greater than 6 or less than 0). I would create a nextDay() method and a previousDay() method that each would return an int, as I noted above.
Now if you wanted to, you could create a constructor that accepted a String and then use this String to set the currentDay value. You would have to loop through your daysOfTheWeek array using a for loop, and then would have to compare the parameter String with each String in the array using String's equals(...) method. If you find a match, then set the currentDay to the array index of the matched String. For instance if the code calls your constructor like so:
then the for loop would loop through the String array, find a match in the 2nd String in the array. The index for the second String is 1 (remember that arrays are zero-based), and so your constructor would set currentDay to = 1.Java Code:Day myDay = new Day("Monday");
Similar Threads
-
How Does One Get Started Learning Java?
By Wataru in forum New To JavaReplies: 29Last Post: 08-08-2009, 10:45 AM -
Just started with Java
By javades in forum New To JavaReplies: 2Last Post: 07-24-2008, 06:39 AM -
getting started
By ash in forum Advanced JavaReplies: 1Last Post: 07-23-2008, 10:31 AM -
Just getting started with java
By DuceDuceExplorer in forum IntroductionsReplies: 4Last Post: 06-29-2008, 06:13 AM -
Getting Started
By Doorsmaniac in forum Java AppletsReplies: 0Last Post: 11-24-2007, 03:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks