Reputation: 57
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
}
Upvotes: 1
Views: 625
Reputation: 124
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
calender calenderObject = new calender();
calender.fillArray();
//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) {
if ((dayOfWeek>=1) && (dayOfWeek<=5)){
calenderObject.createEvent(dayOfWeek, userEvent,dayOfWeek);
for (int i=0;i<calender.length;i++){
System.out.println(calender.events[i]);
}
dayOfWeek = userInput.nextInt();
if (dayOfWeek == -1){
for (int i=0;i<calender.length;i++){
System.out.println(calender.events[i]);
}
System.exit(0);
}
System.out.println("Please type in the name of your event");
userEvent = userInput.next();
} else{
System.out.println("You have entered an invalid number");
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");
dayOfWeek = userInput.nextInt();
}
}
//end main method
}
}
//begin calender class
public class calender {
static int length = 5;
static String[] events = new String[length];
public static String[] fillArray(){
for (int i=0; i< length;i++){
events[i] = "-No event planned-";
}
return events;
}
//begin createEvent method
public String[] createEvent(int l, String userEvent, int dayOfWeek){
int t = l;
if(t%5 == 1 ){
events[0] = ("Monday Appoinment: ") + userEvent;
} else if(t%5 == 2 ){
events[1] = ("Tuesday Appoinment: ") + userEvent;
} else if(t%5 == 3 ){
events[2] = ("WednsdayAppoinment: ") + userEvent;
} else if(t%5 == 4){
events[3] = ("Thursday Appoinment: ") + userEvent;
} else if(t%5 == 0 ){
events[4] = ("Friday Appoinment: ") + userEvent;
}
return events;
}
//end calender class
}
Upvotes: -1
Reputation: 15664
You are declaring a local events
array inside constructor so default value No event planned
is not assigned to your instance variable events
it should be
public calender() {
//Initializes your events array
events = { "-No event planned-",
"-No event planned-",
"-No event planned-",
"-No event planned-",
"-No event planned-"};
//end calender class constructor
}
hope it works
Upvotes: 0
Reputation: 96424
Your main method doesn't know what events
is because there's no local variable named events in scope, and no static variable named events on the main class. You need to tell it you're looking for the events object within a specific calendar object, like this:
System.out.println(calendarObject.events[0]);
This assumes your two classes are in the same package, or that both have no package declared (so they are both in the default package). If the two classes are in different packages you would have to either expose the instance member as public or create a public method that accesses it for the other to see it.
The idea of class-based objects is you can use a class to create multiple objects. That might not come across so well in this assignment since you only create one object of your calendar class. For you to reference a member of an object you need to specify which object you're looking for that member within.
Upvotes: 2
Reputation: 7326
Firstly, it said don't hardcode the size, use a constant. So make a constant like so:
private static final int ARRAY_SIZE = 5;
And change your declaration to
String[] events = new String[ARRAY_SIZE];
And when initializing your array, use a for-counter loop to set each slot to be "-No event planned-", rather than just {some number of "-No event planned"s}, as that isn't dynamic, and won't change with your constant size.
Also, in your constructor remove the type declaration
String[] events
-> events
By declaring the type, you make a new local variable, rather than modifying the field, and so the slots in the array are all still null, as you never modified them, but rather the fields in this local variable.
Upvotes: 1