LearningJava101
LearningJava101

Reputation: 61

Creating Java Program to Return Previous, Current, and Next Day

I am creating a final program for my introductory java programming course and I am in need of some assistance. The current day and the next day values are not correctly printing. I have read over my code again and again but I cannot wrap my head around why this is not running properly. Any help/advice is GREATLY appreciated!

The guidelines I must follow for this program assignment are as follows:

A. Set the day.

B. Print the day.

C. Return the day.

D. Return the next day.

E. Return the previous day.

F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.

G. Add the appropriate constructors.

H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.

I. Write a program to test various operations on the class Day.

*EDIT*** This new code I have written works excellent until I set the day as Monday. The issue I am having is within the previousDay method. I'm trying to add an if/else statement but I cannot get it to compile correctly without receive an outOfBounds Exception error. What is a good way to accomplish this in a String array?

The UPDATED CODE I have written in attempt to accomplish this program task:

public class Day {


int index;

static String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};


public Day(String currentDay) 
{

    for(int i = 0; i < days.length; i++) 
    {

        if(currentDay.equals(days[i])) 
        {

            index = i;

            return;
        }

    }

    System.out.println("Days is invalid");

}

public void printDay() 
{
    System.out.println("The Day is " + days[index]);

}


public String returnDay() 
{
    return days[index]; 

}


public String returnNextDay() 
{
    return days[(index + 1 )% days.length];

}


public String returnPreviousDay() 
{

if (days.length <= 1)

    return days[index + 6];


else return days[(index - 1)%days.length];  
}


public String whatDayIs(int i) 
{
    return days[(index + i)%days.length];

}


public static void main(String[] args) 
{
    Day day = new Day("Mon");

    day.printDay();

    System.out.println("Return Day: " + day.returnDay());

    System.out.println("Next Day: " + day.returnNextDay());

    System.out.println("Previous Day: " + day.returnPreviousDay());

    System.out.println(day.whatDayIs(7));

}

}

Thank you for your time!

Adam

Upvotes: 0

Views: 2960

Answers (7)

user2575725
user2575725

Reputation:

Try updating your method as:

public String returnPreviousDay() {
  return days[(index + days.length - 1) % days.length]; 
}

Here, instead of moving backwards to 0 index, I moved it forward by n - 1 days to get the previous day.

Upvotes: 0

JohnMichael
JohnMichael

Reputation: 1

import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
public static void main(String[] args) {
Day day = new Day();
day.setDay(2);
day.printDay();
day.returnDay1(13);
day.returnDay();
day.nextDay();
day.previousDay(); }}

class Day {

int setday = 0;
int returnday=0;
String x,y;
static String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

void printDay() {

if(setday < 7) {
    System.out.println("The Day is " + days[setday]); }

else
System.out.println("Invalid set of day"); }

void setDay(int dayset) {

setday = dayset; }

void returnDay1(int returnday1) {

returnday = returnday1; }

void returnDay() {

if(setday < 7) {
  x = days[(setday + returnday )% days.length];
  System.out.println("Return Day after "+String.valueOf(returnday)+ " days is " + x); }

else

System.out.println(" "); }

void nextDay() {

if(setday < 7) {

  y = days[(setday + returnday + 1 )% days.length];
  System.out.println("Next Day is " + y); }

else

System.out.println(" "); }

void previousDay() {

if(setday < 7) {

  y = days[(setday + returnday - 1 )% days.length];
  System.out.println("Previous Day is " + y); }

else

System.out.println(" "); } }

Upvotes: -1

Karthikeyan Sukkoor
Karthikeyan Sukkoor

Reputation: 998

You can use the Calendar API with Date. try below code.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class CameoWebServiceClient {
public enum day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

public static void main(String[] args) {

    // read day

    int lDay = 5;
    SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMdd");
    System.out.println(date_format.format(new Date()));
    Calendar cal1 = date_format.getCalendar();

    cal1.set(Calendar.DAY_OF_WEEK, lDay);

    cal1 = setDay(cal1, lDay);
    System.out.println(""+curDay(cal1));
    System.out.println(""+nextDay(cal1));
    System.out.println(""+prevDay(cal1));


}

public static Calendar setDay(Calendar cal1, int lDay) {
    cal1.set(Calendar.DAY_OF_WEEK, (lDay));
    return cal1;
}

public static String curDay(Calendar cal1) {
    int i = cal1.get(Calendar.DAY_OF_WEEK)-1 ;
    i = i < 0 ? 6:i;
    return day.values()[i]+"";



}

public static String nextDay(Calendar cal1) {
    int i = cal1.get(Calendar.DAY_OF_WEEK);
    i = i > 6 ? 0:i;
    return day.values()[i]+"";

}

public static String prevDay(Calendar cal1) {
    int i = cal1.get(Calendar.DAY_OF_WEEK)-2;
    i = i < 0 ? 6:i;
    return day.values()[i]+"";

}
}

Upvotes: 0

Ingo
Ingo

Reputation: 36349

Your code is full of problems, problems that stem from a deep non-understanding of Java.

For example, look at this:

public Day setNameDay(String Day) {
    Day = Days;
    return this;
}

It is not only the naming that is bad. What do you think this method actually does?

I seriuously suggest you take the course again, and pay attention when topics like parameter passing etc. come up.

Upvotes: 2

Paul Samsotha
Paul Samsotha

Reputation: 209072

"The current day and the next day values are not correctly printing."

nextDay() has no print statement

public void nextDay()
{
    Day++;

    if (Day <8)
        Day = 1;
    setDay(Day);
    }

And you actually need to call setDay() before Days has a value;

Day myDay = new Day();
myDay.setDay(1);
myDay.printDay();

And the fact that you have a class Day, and int Day and String Days is totally confusing for anyone who is reading your code. I'm surprised you're even able to follow it. Consider Java naming convention: variable names start with lower case.

Upvotes: 2

BobTheBuilder
BobTheBuilder

Reputation: 19294

First of all, use java naming conventions. It makes your code clearer and easier to help.

You have some errors in your code:

public Day setNameDay(String Day) {
    Day = Days;
    return this;
}

What you do here is saving Days value in method's parameter Day which actually does nothing.

public void nextDay() {
    Day++;

    if (Day <8)
        Day = 1;
    setDay(Day);
}

Why setting day to one if it is lower than 8? You should use:

    if (Day >8) {
        Day = 1;
    }

Upvotes: 2

Tim B
Tim B

Reputation: 41208

You are trying to re-invent something that already exists and it is making your life harder.

Simply store the data in a Calendar object. Use dateAdd to add and remove days, read the result.

Upvotes: -1

Related Questions