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: 1st Assignment Sem 2 Counter + GUI

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 1st Assignment Sem 2 Counter + GUI

    Hey everyone, Semester 2 of my University course and they have decided to give us an assignment involving returning values from a method without actually teaching us how to do it...brilliant.

    Anyway heres, I won't bother putting the GUI code and such but heres the code for the buttons and what they are "supposed to do."

    Any help would be appreciated and any links to reading material would also be appreciated.

    This is the code for the Logic class, i'm not supposed to touch this.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Logic
    {
      private Counter counter = null;
     
      public Logic( Counter aCounter )
      {
        counter = aCounter;
      }
     
      public long getValue()
      {
        return counter.getValue();
      }
     
      public void process( String button )
      {
        String info = null;
        if ( button.equals( "Inc" ) )
        {
            counter.inc();
        }
        if ( button.equals( "Dec" ) )
        {
            counter.dec();
        }
        if ( button.equals( "Zero" ) )
        {
            counter.reset();
        }
      }
     
    }

    Heres the code for the Counter class, this is what i'm supposed to mess with but I can't figure out how to use the GetValue method to use the other methods to plus, minus and reset using a long num variable.

     
    class Counter
    {
      //Return the value of the counter
      public long getValue(){
          //ADVICE ON WHAT GOES HERE :S
        }
      //Reset the value of the counter to 0
      public void reset( ){
          long num = 0; 
        }
      //Increment the counter by 1
      public void inc(){
          long num = + 1;
        }
      //Decrement the counter by 1
      public void dec(){
          long num = - 1;
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 1st Assignment Sem 2 Counter + GUI

    Word to the wise: don't waste time complaining about your teacher or course. Plenty of people learn without all of the aid that comes with a school-based education (tutors, peers, books, etc).

    Recommended reading: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 1st Assignment Sem 2 Counter + GUI

    Quote Originally Posted by KevinWorkman View Post
    Word to the wise: don't waste time complaining about your teacher or course. Plenty of people learn without all of the aid that comes with a school-based education (tutors, peers, books, etc).

    Recommended reading: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Thanks for the reading material, I completely understand about not wasting time complaining about a teacher however when said teacher forces you to attend lectures/seminars and spends around 2 hours talking about his past history with programming, the history of Apple and Microsoft then asks you to do some coursework for him without even explaining the material beforehand...it gets frustrating.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 1st Assignment Sem 2 Counter + GUI

    You're preaching to the choir on that one. But try reading through the tutorial and taking another look at the assignment. If you're still having trouble, post what you've tried along with any errors you get, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 1st Assignment Sem 2 Counter + GUI

    Quote Originally Posted by KevinWorkman View Post
    You're preaching to the choir on that one. But try reading through the tutorial and taking another look at the assignment. If you're still having trouble, post what you've tried along with any errors you get, and we'll go from there.
    Went out last night instead of doing thus but sat down this morning and read through all the advice here and the links, sucessfully got it working

     
    class Counter
    {
    public int count = 0;
        //Return the value of the counter
        public long getValue(){
            return count;
        }
        //Reset the value of the counter to 0
        public void reset(){
            count = 0;
        }
        //Increment the counter by 1
        public void inc(){
            count++;
        }
        //Decrement the counter by 1
        public void dec(){
            count--;
        }
    }

    Very simple in the end, never been told about public variables and such.

Similar Threads

  1. Counter troubles
    By GinoGore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 09:28 AM
  2. Help With Counter
    By Catgroove in forum Java Theory & Questions
    Replies: 7
    Last Post: February 10th, 2011, 08:50 AM
  3. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 11:49 AM
  4. Odd Even Zero Counter?
    By velop in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:13 PM
  5. String counter
    By chkang0130 in forum Algorithms & Recursion
    Replies: 9
    Last Post: December 8th, 2009, 11:56 AM