Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

Java programming problem

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
Home » Discuss » DU Groups » Computers & Internet » Website, DB, & Software Developers Group Donate to DU
 
whatsername Donating Member (45 posts) Send PM | Profile | Ignore Sun Dec-11-05 01:33 AM
Original message
Java programming problem
If anyone is willing....I need some help with a homework assignment.

We are writing a program that creates a simple personal calender/schedule.

We have three classes:
Schedule()
BusyTime(ElapsedTime start, ElapsedTime duration)
ElapsedTime (int days, int hours, int minutes)

I have a pretty good idea where I am going once I get into it, but I am stuck on the toString of my 2D array in Schedule(). I can't find any example of how to do this as in my book all of the 2D arrays are printed out in the main method using System.out.println. In the output below, you can see that I've managed only to print the actual labels and have not even got them quite right. At this point I just want it to print out X's (null) for each time slot in the schedule.



public class Schedule {


//private int i= 0;
//private int j = 0;


public Schedule()
{
BusyTime [][] table = new BusyTime<28><24>;

for (int i = 0; i < 24; i ++)
for (int j = 0; j < 28 ; j ++);

}




public BusyTime firstFitInsert (BusyTime b)
{
return b;

}




public String hours()
{
int hours = 0;
String hourLabels = null;

if (hours <= 13)
hours ++;
hourLabels = hours + ":00 \n";
return hourLabels;



}




public String toString()
{

String dayLabels = " S M T W T F S S M T W T F S S M T W T F S S S M T W T F S\n12:00";
System.out.println(dayLabels);

int hours = 0;
String hourLabels = null;

while (hours < 12)
{ hours ++;
hourLabels = " " + hours + ":00";
System.out.println (hourLabels);
}





return null;




}
}

OUTPUT:
S M T W T F S S M T W T F S S M T W T F S S S M T W T F S
12:00
1:00
2:00
3:00
4:00
5:00
6:00
7:00
8:00
9:00
10:00
11:00
12:00
Refresh | 0 Recommendations Printer Friendly | Permalink | Reply | Top
aePrime Donating Member (676 posts) Send PM | Profile | Ignore Sun Dec-18-05 07:33 PM
Response to Original message
1. Here's an example.
Well, I don't have your BusyTime or ElapsedTime classes, but here's an example of printing out a 2D array. It's just nested loops. I used Java 5's new format option to make it pretty.

Your toString function shouldn't actually print anything out at all when it's done -- just return a string.

Of course, if I were really doing this, I'd probably just use Java's date classes.



public class Schedule
{
private final int HOURS_IN_DAY = 24;
private int daysInMonth;
private char[][] myArray;

public Schedule(int daysInMonth)
{
this.daysInMonth = daysInMonth;

myArray = new char;
for (int day = 0; day < daysInMonth; ++day) {
for (int hour = 0; hour < HOURS_IN_DAY; ++hour) {
myArray = 'X';
}
}
}

public String toString()
{
// This is quite clearly a hack to get the days of the week to line up with
// the printed schedule. If I had taken more time, these numbers wouldn't
// be hard-coded, but who wants to format a format string? Ick.
String retString = " ";

// This function makes use of Java 5's format option. Yay!

for (int i = 0; i < daysInMonth; ++i) {
retString += String.format("%2c", translateNumberToWeekday(i));
}

for (int hour = 0; hour < HOURS_IN_DAY; ++hour) {
retString += String.format("\n%8s ", translate24to12(hour));
for (int day = 0; day < daysInMonth; ++day) {
retString += String.format("%2c", myArray);
}
}

return retString;
}

private String translate24to12(int hour)
{
if (hour == 0)
return "Midnight";
if (hour == 12)
return "Noon";

if (hour > 12)
return (hour - 12) + ":00 PM";
else
return hour + ":00 AM";
}

private char translateNumberToWeekday(int day)
{
final int val = day % 7;
switch (val) {
case 0:
return 'S';
case 1:
return 'M';
case 2:
return 'T';
case 3:
return 'W';
case 4:
return 'R';
case 5:
return 'F';
case 6:
return 'A';
}

// If we get an X, something is very wrong.
return 'X';
}
}
Printer Friendly | Permalink | Reply | Top
 
whatsername Donating Member (45 posts) Send PM | Profile | Ignore Sat Dec-24-05 11:30 PM
Response to Reply #1
2. Hey Thanks!
I wish I had checked back sooner. This assignment was past due and I turned it in incomplete. Some of the stuff you show me is too advanced for this beginner course. But I really appreciate the reply. I start up again in January with the next Java class, though, so I plan on finishing this assignment just so I know that I know what I'm doing.
I ended up with a 2D array of null BusyTime objects that printed out as follows:

SMTWTFSSMTWTFSSMT......
------------------
------------------

BusyTime@djlkf664
BusyTime@fjdi6sld



and so on for the month. Obviously my toString() wasn't returning what is should. Just pointing.

I really love doing this and I had a solid 'A' until this last assignment and the final. I'll still pass but it hurts me alot (mentally and academically)! Just the holiday season and being so pressed for time.

Thanks again, I'm sure I'll be back next semester asking for more help!
-L

Printer Friendly | Permalink | Reply | Top
 
DU AdBot (1000+ posts) Click to send private message to this author Click to view 
this author's profile Click to add 
this author to your buddy list Click to add 
this author to your Ignore list Sun Dec 22nd 2024, 09:47 AM
Response to Original message
Advertisements [?]
 Top

Home » Discuss » DU Groups » Computers & Internet » Website, DB, & Software Developers Group Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC