FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » Basic Programming help

   
Author Topic: Basic Programming help
Philosofickle
Member
Member # 10993

 - posted      Profile for Philosofickle           Edit/Delete Post 
Hey, I've moved on from programming and am taking a java class that's giving me a bit of trouble. The Parameters of the assignment are thus:

Create a class called 'Date' that includes three pieces of information as instance variables: a month (type int), a day (type int), and a year (type int). Your class should have a constructor that initializes the three instance variables and a default constructor that sets the date to Jan 1, 1970. Provide a set and get method for each instance variable. Make sure that any values passed into the constructor represent a valid date (don't allow Feb 29 unless it's a leap year, don't allow zeros for day or month, don't allow values greater than 12 for months, etc). Provide two output methods, one that outputs the date as a string in the form of 'month/day/year' and another than outputs the date as a string in the form of 'day month-name, year'. Document all public methods with appropriate comments.

I haven't made much progress because the teacher has a "Make them try it before I explain anything" approach, and the textbook isn't helping.

Posts: 208 | Registered: Sep 2007  |  IP: Logged | Report this post to a Moderator
Alcon
Member
Member # 6645

 - posted      Profile for Alcon   Email Alcon         Edit/Delete Post 
Do you know how to write a basic java class? What do you have so far? What does the text book say on the matter, and what in specific are you stuck on?

Editted to add, if it's basic syntax you're having trouble with, that's easy enough to lay out.

Here's the basic layout of a Java class's syntax:

code:
public class Date {
/* Your constructors go here. */

/* Your accessors go here. */

/* Your private variables go here. */
}

To declare a constructor you declare a public method with no return type who's name is the name of the class. For example:

code:
public Date() {
/* Do something in here... */
}

To declare a public method the basic syntax goes like this:

code:
public <return type> <method name>(<parameters) {
/* Do something in here... */
}

For example:

code:
public int getMyInt() {
return MyInt;
}

The basic syntax to declare a private variable goes along these lines:

code:
private int MyInt;

And honestly what I've just told you pretty much solves the whole problem for you (which makes me want to growl at your professor, cause that's a really lame assignment).
Posts: 3295 | Registered: Jun 2004  |  IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
What help are you looking for?
Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
TomDavidson
Member
Member # 124

 - posted      Profile for TomDavidson   Email TomDavidson         Edit/Delete Post 
Do you know what a constructor is?
Posts: 37449 | Registered: May 1999  |  IP: Logged | Report this post to a Moderator
Philosofickle
Member
Member # 10993

 - posted      Profile for Philosofickle           Edit/Delete Post 
The Main page so far has:

public class TellTime {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
DateExample d1 = new DateExample();
DateExample d2 = new DateExample(1,29,2009);
System.out.println(d1);
System.out.println(d2);
System.out.println(d2.toString());
// TODO code application logic here
}

}


And I've put in another class that consists of:

public class DateExample {
//declare instance variables to tell how we want to show the date
private int day;//these are instance variables
private int month;
private int year;

public DateExample(){
this(1,1,2009);

}

public DateExample(int month, int day, int year){
this.month = month;
this.day = day;
this.year = year;
}

public String monthName(){
if(month==1){
return "January";
}
if(month==2){
return "February";
}
if(month==3){
return "March";
}
if(month==4){
return "April";
}
if(month==5){
return "May";
}
if(month==6){
return "June";
}
if(month==7){
return "July";
}
if(month==8){
return "August";
}
if(month==9){
return "September";
}
if(month==10){
return "October";
}
if(month==11){
return "November";
}
if(month==12){
return "December";
}
return "What the?";
}
@Override
public String toString(){
return String.format("%d/%d/%d)", month, day, year);
}

}

What I can't figure out is how to put in the code that will let the user input the date, and the code that will make it so that a user can put in a Feb. 29 on years divided by 4 unless also divisible by 100 unless also divisible by 400

Posts: 208 | Registered: Sep 2007  |  IP: Logged | Report this post to a Moderator
Philosofickle
Member
Member # 10993

 - posted      Profile for Philosofickle           Edit/Delete Post 
That's all I have so far, I've figured out how to make it output the dates that I give it, is how do I get dates from the user, and the limiting formula is driving me crazy!
Posts: 208 | Registered: Sep 2007  |  IP: Logged | Report this post to a Moderator
MattP
Member
Member # 10495

 - posted      Profile for MattP   Email MattP         Edit/Delete Post 
I don't see anything in your first post about a user interface being required. Does the user need to be able to interactively input dates? From the assignment it just sounds like your class needs to throw an exception if invalid parameters are provided to the constructor. That's really the only way to "not allow" invalid date values passed to a constructor.*

*EDIT: Though, if user input is required, then I guess they could mean you should check to see if the values are correct before calling the constructor. It's just that user input is not explicit in the assignment text you've posted.

Posts: 3275 | Registered: May 2007  |  IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
I'd recommend learning to use switch statements. They'll help out a bit with the long strings of conditional statements.

The limiting method would work really well with a case statement. You should be able to do it with 3 groups of cases and an exception throwing default. (edit: and an if statement.)

---

Let's say I've got 5 rooms and I need to check one thing for rooms 1, 2, 5 and something else for rooms 3, 4. I could do that with a switch statement like so:
code:
switch (roomNum){
case 1:
case 2:
case 5:
if (!hasBlueLevelAccess(person){
throw new Exception("Access Denied");
}
case 3:
case 4:
if (!isBearingCookies(person){
throw new Exception("Eat them");
}
default:
throw new Exception("No Such Room. Killer robots dispatched.");
}



[ January 30, 2009, 05:14 PM: Message edited by: MrSquicky ]

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
Do you understand exceptions?
Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2