-1

I am limited to using the specified instance variables, and the specified methods. How can I create 2 DateTime objects (one for current date, and one for future date) while using these same methods for both. currentDate and futureDate should have different values, but I can't figure out how to do that while using only these methods and variables.

public class DateTime {  //Enter and calculate data time

private int month; //Month instance variable
private int day; // Day instance variable
private int hour; // Hour instance variable
private int minute; // Minutes instance variable
private int second; // Second instance variable
private Scanner input = new Scanner(System.in); // Instance variable for all inputs



public void inputMonth() { // Input month value
    System.out.print("Enter the month (1-12): ");
    month = input.nextInt();
    }

public void inputDay() { // Input days value
    System.out.print("Enter the day (1-30): ");
    day = input.nextInt();
    }

public void inputHours() { // Input hours value
    System.out.print("Enter the hour (0-23): ");
    hour = input.nextInt();
    }

public void inputMinutes() { // Input minutes value
    System.out.print("Enter the minutes (0-59): ");
    minute = input.nextInt();
    }

public void inputSeconds() {  //  Input seconds value 
    System.out.print("Enter the seconds (0-59): ");
    second = input.nextInt();
    }
}
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
duleejones
  • 11
  • 1
  • I don't understand your reference to two values and one instance variable. You say you're making two `DateTime` objects. Why would you try to assign them both to the same variable? – azurefrog Oct 12 '17 at 19:38
  • That being said, mixing the input and the data together the way you are is... weird. You'd be much better off having normal constructor/setter methods and extracting the user input to somewhere else. – azurefrog Oct 12 '17 at 19:39
  • why not just make it into array – Evinn Oct 12 '17 at 19:39

1 Answers1

-1
Create a new Class and create 2 objects of your DateTime Class as given 
below:
class Runner
{
    DateTime currentDate = new CurrentDate();
    currentDate.inputMonths();
    currentDate.inputDays();
    currentDate.inputHours();
    currentDate.inputMinutes();
    currentDate.inputSeconds();

//Now Create Another Object of DateTime Class:
    DateTime futureDate = new CurrentDate();
    futureDate.inputMonths();
    futureDate.inputDays();
    futureDate.inputHours();
    futureDate.inputMinutes();
    futureDate.inputSeconds();
}//end of class

Create ShowDate method in your DateTime class and call it with both the objects.
  • You want to break you comment so that it doesn't look like just one huge code block. – Paul Wagland Oct 12 '17 at 20:27
  • it's wrong to call non static methods inside class body. – Rahmat Waisi Oct 12 '17 at 22:01
  • @RahmatWaisi : You can call non-static method by creating objects of class. For static methods you don't need to create objects, you simply can call static object with class reference just like ClassName.staticMethod()....! Correct Me If I am Wrong – Muhammad Mansha Dar Oct 13 '17 at 04:31
  • @MuhammadManshaDar you are allowed to call methods in other methods/constructors *OR* in static block *OR* non-static block of code([read this](https://stackoverflow.com/a/2420466/4101906) and also [this](https://stackoverflow.com/a/28977888/4101906) to learn more about methods and blocks of code), not directly in class body. otherwise you`ll get compile errors. – Rahmat Waisi Oct 13 '17 at 13:29