Why can't my main class see the array in my calender class
Posted
by
Rocky Celltick Eadie
on Stack Overflow
See other posts from Stack Overflow
or by Rocky Celltick Eadie
Published on 2012-10-06T14:38:51Z
Indexed on
2012/10/06
15:37 UTC
Read the original article
Hit count: 310
This is a homework problem. I'm already 5 days late and can't figure out what I'm doing wrong.. this is my 1st semester in Java and my first post on this site
Here is the assignment..
Create a class called Calendar
. The class should contain a variable called events
that is a String array. The array should be created to hold 5 elements. Use a constant value to specify the array size. Do not hard code the array size. Initialize the array in the class constructor so that each element contains the string “ – No event planned – “.
The class should contain a method called CreateEvent
. This method should accept a String argument that contains a one-word user event and an integer argument that represents the day of the week. Monday
should be represented by the number 1
and Friday
should be represented by the number 5
. Populate the events array with the event info passed into the method. Although the user will input one-word events, each event string should prepend the following string to each event:
event_dayAppoinment: (where event_day is the day of the week)
For example, if the user enters 1 and “doctor” , the first array element should read: Monday Appointment: doctor
If the user enters 2 and “PTA” , the second array element should read: Tuesday Appointment: PTA
Write a driver program (in a separate class) that creates and calls your Calendar class. Then use a loop to gather user input. Ask for the day (as an integer) and then ask for the event (as a one word string). Pass the integer and string to the Calendar object’s CreateEvent
method. The user should be able enter 0 – 5 events. If the user enters -1
, the loop should exit and your application should print out all the events in a tabular format. Your program should not allow the user to enter invalid values for the day of the week. Any input other than 1 – 5
or -1
for the day of the week would be considered invalid.
Notes:
When obtaining an integer from the user, you will need to use the nextInt()
method on your Scanner object. When obtaining a string from a user, you will need to use the next()
method on your Scanner object.
Here is my code so far..
//DRIVER CLASS
/**
*
* @author Rocky
*/
//imports scanner
import java.util.Scanner;
//begin class driver
public class driver {
/**
* @paramargs the command line arguments
*/
//begin main method
public static void main(String[] args) {
//initiates scanner
Scanner userInput = new Scanner (System.in);
//declare variables
int dayOfWeek;
String userEvent;
//creates object for calender class
calendercalenderObject = new calender();
//user prompt
System.out.println("Enter day of week for your event in the following format:");
System.out.println("Enter 1 for Monday");
System.out.println("Enter 2 for Tuesday");
System.out.println("Enter 3 for Wednsday");
System.out.println("Enter 4 for Thursday");
System.out.println("Enter 5 for Friday");
System.out.println("Enter -1 to quit");
//collect user input
dayOfWeek = userInput.nextInt();
//user prompt
System.out.println("Please type in the name of your event");
//collect user input
userEvent = userInput.next();
//begin while loop
while (dayOfWeek != -1) {
//test for valid day of week
if ((dayOfWeek>=1) && (dayOfWeek<=5)){
//calls createEvent method in calender class and passes 2 variables
calenderObject.createEvent(userEvent,dayOfWeek);
} else {
//error message
System.out.println("You have entered an invalid number");
//user prompts
System.out.println("Press -1 to quit or enter another day");
System.out.println("Enter 1 for Monday");
System.out.println("Enter 2 for Tuesday");
System.out.println("Enter 3 for Wednsday");
System.out.println("Enter 4 for Thursday");
System.out.println("Enter 5 for Friday");
System.out.println("Enter -1 to quit");
//collect user input
dayOfWeek = userInput.nextInt();
//end data validity test
}
//end while loop
}
//prints array to screen
int i=0;
for (i=0;i<events.length;i++){
System.out.println(events[i]);
}
//end main method
}
}
/**
*
* @author Rocky
*/
//imports scanner
import java.util.Scanner;
//begin calender class
public class calender {
//creates events array
String[] events = new String[5];
//begin calender class constructor
public calender() {
//Initializes array
String[] events = {"-No event planned-","-No event planned-","-No event planned-","-No event planned-","-No event planned-"};
//end calender class constructor
}
//begin createEvent method
public String[] createEvent (String userEvent, int dayOfWeek){
//Start switch test
switch (dayOfWeek){
case 1:
events[0] = ("Monday Appoinment:") + userEvent;
break;
case 2:
events[1] = ("Tuesday Appoinment:") + userEvent;
break;
case 3:
events[2] = ("WednsdayAppoinment:") + userEvent;
break;
case 4:
events[3] = ("Thursday Appoinment:") + userEvent;
break;
case 5:
events[4] = ("Friday Appoinment:") + userEvent;
break;
default:
break;
//End switch test
}
//returns events array
return events;
//end create event method
}
//end calender class
}
© Stack Overflow or respective owner