-2

I am creating a new Java class called DateTwo and am trying to declare and initialize a member field for this class called dayNumber, which will be a number between 1 and 7.

I have Tried:

public class DateTwo {
    int dayNumber = 1;
    int dayNumber = 2;
    int dayNumber = 3;
    int dayNumber = 4;
    int dayNumber = 5;
    int dayNumber = 6;
    int dayNumber = 7;
}

Error Message:

Variable dayNumber is already defined in class DateTwo

public class DateTwo {
    int dayNumber = (>=1 && <=7);
}

Error Message:

illegal start of expression

Any thoughts or Ideas how to fix this error?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Michael Oldham
  • 63
  • 1
  • 10
  • 4
    You need to look into random number generation in Java. I would recommend reading this question (http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java) – MarshallOfSound Aug 26 '15 at 03:16
  • 1
    Are you sure you clearly understand what they're asking you to do? before writing code and all that. – Alfabravo Aug 26 '15 at 03:25
  • @Alfabravo do not think so . the op just looked for a ready answer and he got it all. – Kick Buttowski Aug 26 '15 at 03:40

4 Answers4

1

You declare a class with a member field, make the field private so nobody else can touch it, and implement a get and a set method, also called a "getter" and a "setter". The setter should validate.

You might also want a constructor, so the field is never uninitialized.

public class DateTwo {
    private int dayNumber;

    // Constructor
    public DateTwo(int dayNumber) {
        setDayNumber(dayNumber); // Reuses setter to enforce validation
    }

    // Setter
    public void setDayNumber(int dayNumber) {
        if (dayNumber < 1 || dayNumber > 7)
            throw new IllegalArgumentException("Invalid day number: " + dayNumber);
        this.dayNumber = dayNumber;
    }

    // Getter
    public int getDayNumber() {
        return this.dayNumber;
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

You apparently want just one value (not an array of days).

This code will do that:

import java.util.Random;

public class DateTwo {

  private dayNumber;

  // Initialize all class state in your "constructor"
  public DateTwo () {
    // this gives you a random integer between 0 and 6
    dayNumber = new Random().nextInt(7);
    // this shifts the value to between 1 and 7
    dayNumber += 1;
  }

  // This "getter" method allows clients to get your (private) dayNumber
  public getDayNumber () {
    return dayNumber;
  }

}

A simpler way to accomplish the same thing:

dayNumber = new Random().nextInt(7) + 1;

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Nothing in the question said anything about randomly choosing a number, just that the number has to be within a range, aka be valid. – Andreas Aug 26 '15 at 03:29
0

@KickButtowski Just for your information, I am not a plagiarizer. I am also not in school. I used his code as a reference for my code: (Without Comments)

Thank you everyone who helped me to come to this conclusion.

(Without Comments)

public class DateTwo {
    public int dayNumber = 1;


    public void displayDay() {
        if (dayNumber >= 1 && dayNumber <= 7)
            if (dayNumber >=1 && dayNumber <=7)
                System.out.println("That is a day of the week.");
            if (dayNumber == 1)
                System.out.println("It is: Monday");

            else if (dayNumber == 2) {
                System.out.println("It is: Tuesday");
            }
            else if (dayNumber == 3){
                System.out.println("It is: Wednesday");
            }
            else if (dayNumber == 4){
                System.out.println("It is: Thursday");
            }
            else if (dayNumber == 5){
                System.out.println("It is: Friday");
            }
            else if (dayNumber == 6){
                System.out.println("It is: Saturday");
            }
            else if (dayNumber == 7){
                System.out.println("It is: Sunday");
            }
        else {
            System.out.println("That is not a Day of the Week.");
        }
    }
}
Michael Oldham
  • 63
  • 1
  • 10
  • Use a `switch` statement, your indentations are off, and you run the same `if` twice at the beginning. – Andreas Aug 26 '15 at 06:04
-1

Currently you are redefining already defined variable again & again. So, the error is coming. Use array

public class DateTwo {
    int dayNumber[]=new int[7];//declaration and instantiation  
    dayNumber[0] = 1;
    dayNumber[1] = 2;
    dayNumber[2] = 3;
    dayNumber[3] = 4;
    dayNumber[4] = 5;
    dayNumber[5] = 6;
    dayNumber[6] = 7;
}

or

int dayNumber[]={1,2,3,4,5,6,7};//declaration, instantiation and initialization 
Harshit
  • 5,147
  • 9
  • 46
  • 93