Search code examples
javaconstructorbluej

My constructor is not taking the variables


My constructor does not take in variables. The user runs class Clock, and in theory, the entered hour and minute will be passed to class Time. Unfortunately, the variables hour and minute are not passed to Time, and the code does not get carried out.

This is the first class, where the user inputs the current time.

/**
 * Write a description of class Clock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

import java.util.Scanner;

public class Clock
{
public static void main (String [ ] args)
{
    Scanner scan = new Scanner (System.in);
    int hour = 12;
    int minute = 00;

    System.out.println("Please enter the hour.");
    System.out.println();

    System.out.print("-->  ");
    hour = scan.nextInt();

    System.out.println();
    System.out.println();

    System.out.println("Please enter the minute.");
    System.out.println();

    System.out.print("-->  ");
    minute = scan.nextInt();

    System.out.println();
    System.out.println();
    System.out.println();

    if (hour > 24 || hour < 00)
    {
        hour = 24;
    }

    if (minute > 59 || minute < 00)
    {
        minute = 00;
    }

    Time CLOCK = new Time ();

    System.out.println(CLOCK);
}

}

And this is the second class, which adds one minute to the time entered, prints it in standard time, and prints it in military time.

/**
 * Write a description of class Time here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Time 
{
private int h;
private int m;

String string4;
String string7;
String mitime;

public void increment (String string1, int minute)
{
    int intime = minute + 1;
    String string3 = intime + "";
    string4 = string1+string3;
}

public void convert (int h, int m)
{
    int hour = h;
    int minute = m;

    String zone = "";

    if (hour > 12)
    {
        zone = "PM";
    }
    else
    {
        zone = "AM";
    }

    if (hour > 12)
    {
        hour = hour - 12;
    }

    String string5 = hour + "";
    String string6 = minute + "";
    string7 = hour + ":" + minute + " " + zone;

}

public void clock (int hour, int minute)
{
    h = hour;
    m = minute;


    convert(h, m);

    int integer = hour;

    String string1 = integer + "";

    int integer2 = minute;


    String string2 = integer2 + "";

    mitime = integer + string2;

    increment(string1, minute);
}

public String toString ( )
{
    return "Military Time:\n" + 
            mitime +
            "\n\nStandard Time:\n" +
            string7 +
            "\n\nIn one minute, it will be " +
            string4;
}

}

Would you be able to point out where I went wrong, and how I could fix this error?

Thank you very much.


Solution

  • Your Time object needs a constructor if you want to pass values to it:

    public class Time{
        public Time(int value1, int value2) {
          h = value1;
          m = value2;
        }
    }
    

    Or something like that :) If you want to create a new time instance, you call your constructor

    new Time(3, 4)
    

    Also, remember that int values are normal integers. You can not store 00 or 01, they will be stored as 0 and 1. If you want to view your minutes as 00 or so, you need to figure out a way to display the values in a nice way!