Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem calling

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Problem calling

    Hey!


    I have a two classes and i have a rather hard time calling the second last method from the first class
    so that the last method, which is a private, can react!
    Not allowed to change the first class but the second one i can do as i please but no matter what i
    still cannot get the right call from it.

    This is the one that i'm not allowed to change.

    public class AlarmClock {
     
    	/* Current time */
    	private int hours = 0;
    	private int minutes = 0;
    	/* Alarm Properties */
    	private int alarmHour = 0;
    	private int alarmMinute = 0;
    	private boolean alarmOn = false;
     
    	public AlarmClock(int h, int m) {
    		hours = h;
    		minutes = m;
    	}
     
    	public void displayTime() {
    		System.out.println("Time: " + hours + " hours, " + minutes + " minutes");
    	}
     
    	public void setAlarm(int h, int m) {
    		alarmHour = h;
    		alarmMinute = m;
    		alarmOn = true;
    	}
     
    	public void displayAlarmTime() {
    		System.out.println("Alarm Time: " + alarmHour + " hours, " + alarmMinute + " minutes");
    	}
     
    	public void timeTick() {
    		minutes = minutes + 1;
     
    		if (minutes == 60) {
    			hours = hours + 1;
    			minutes = 0;
    		}
     
    		if (hours == 24) {
    			hours = 0;
    		}
     
    		checkAlarm();
    	}
     
    	private void checkAlarm() {
    		if (alarmOn && minutes == alarmMinute && hours == alarmHour)
    			System.out.println("Wake up! Time to go!");
    	}
     
    }

    And this is my main.
    public class AlarmMain {
     
    	public static void main(String[] args) {
     
    		AlarmClock A = new AlarmClock(23, 48);
    		A.setAlarm(6, 15);
    		A.displayTime();
    		A.displayAlarmTime();
    		A.timeTick();
    	}
    }

    And what i need to be able to do is to add 500 more min so that the call from "A.timeTick()" reacts.
    The time that is set is 23:48.
    I have tried to add more time onto the "new AlarmClock(h, m)" by adding
    on m within a forloop but that didn't work.

    Any idea as of how i should go about fixing this?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,467
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Problem calling

    add 500 more min so that the call from "A.timeTick()" reacts.
    Where is the 500 minutes passed to the method? Should it be a parameter to the method?
    Right now the method is hardcoded to add 1 to the minutes. Is that what it is supposed to do?
    To add 500 minutes, it would have to be called 500 times.

    calling the second last method from the first class
    It would be very helpful if you gave the name of the classes and the names of the methods instead of making a reader guess what ones you are talking about. second and first are not 100% clear
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Problem calling

    Quote Originally Posted by Norm View Post
    Where is the 500 minutes passed to the method? Should it be a parameter to the method?
    Right now the method is hardcoded to add 1 to the minutes. Is that what it is supposed to do?
    To add 500 minutes, it would have to be called 500 times.
    I made it without anything saying that "we are adding 500 min" because everytime i tried it didn't work.

    Well i didn't get anything good from adding 500 min there and i don't think that i should call something 500 times.. maybe forloop that but
    it doesn't seem right.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,467
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Problem calling

    What does the 500 min have to do with the program's requirements?

    If the method only adds 1 each time it is called, then to add 500 it needs to be called 500 times.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Problem calling

    Quote Originally Posted by Norm View Post
    What does the 500 min have to do with the program's requirements?

    If the method only adds 1 each time it is called, then to add 500 it needs to be called 500 times.
    Sorry about that. It worked. Yeah i get what you mean now.
    Added a forloop and made it do the method 500 times. Works fine!
    Thanks.

    PS: Added an if statement within the loop to break if the int of i is greater than
    or equal to 500. That didn't actually change anything but
    just in case i guess.

Similar Threads

  1. problem: calling a function in Intellij (Beginner)
    By yeezreal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 18th, 2018, 03:05 PM
  2. problem in calling a method
    By quasarLie in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 14th, 2018, 08:59 AM
  3. Problem with calling objects from same-class methods in main
    By thekbon in forum Object Oriented Programming
    Replies: 12
    Last Post: December 18th, 2012, 01:10 AM
  4. Problem while calling mPlayer from Java application
    By bhavani418 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 28th, 2012, 10:49 PM
  5. Problem with onModuleLoad() calling-GWT
    By IliaPrikhodko in forum Web Frameworks
    Replies: 1
    Last Post: August 11th, 2011, 07:12 AM