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 8 of 8

Thread: New to programming

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    Brunei
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default New to programming

    hi, I am very new to programming and i need help on on this question
    "a table have 4 legs, a stool have 3 leg, given 300 legs, find number of tables and stools."

    it is to be done with public static void main(String[] args){
    I started recently with the class so we only know so much to methods,Conditionals and recursion.

    I have done quadratic equations with java. so i may know a thing or two on variables and math methods.
    the thing is this question has an equation with two variables. I am to list down the number of stools and tables with System.out.print

    so, can anyone tell me what i need to know or just how to start out on this question?

    I apologize for my english
    thank you in advance


    edit:i posted on the wrong section , you can still help me out here
    Last edited by Cranky; August 22nd, 2011 at 07:54 AM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: New to programming

    Well you don't have a quadratic equation there, what you have is 4t + 3s = 300 - it's the same kind of equation (y = mx + c) that's used to specify a straight line on a graph. You can't draw a line because you have to find an integer solution (half a table is no good to anyone). You would expect a set of pairs of t and s which would each satisfy the equation. If you had to solve this equation by hand, how would you start? I think once you use a pencil and paper on this problem, you will quickly think of something to code.

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: New to programming

    Seems to me that there are multiple solutions to this.

    The pen and paper version:

    1) 96 stools and 3 tables( (96*3)+(3*4) = 300)
    2) 92 stools and 6 tables( (92*3)+(6*4) = 300)
    3) 88 stools and 9 tables
    ...ect.

    I find it interesting that the number of tables with 4 legs will be multiple of 3, and the number of stools with 3 legs will be a multiple of 4.

    Start here:
    public class TableStool
    {
      public static void main(String[] args)
      {
    //continue writing code
      }
    }

    I never got as far as quadratics in math, I just looked it up on wiki looks like Greek to me.

    However I can think of a way to do it without any complex math.

    basically start with a do...while loop:

    init your variables first:

    Start with 3 tables and less than 4 stools will not satisfy the equation:

    int legsLeft, stools, tables = 3;
    do
    {
    //more code goes here
    }while (stools > 3);

    inside this loop, starting with the 3 tables, subtract that number of legs from 300:
    then divide the result by the number of legs in stools:

    legsLeft = 300 - (4 * tables);
    stools = legsLeft / 3;
     
    //OR skip the legsLeft variable:
     
    stools = (300 - (4 * tables))/3;

    Then setup a println to display the number of tables and stools.

    After the println, add 3 more tables, it has to be a multiple of 3:

    tables += 3;

    And then let the do...while do the rest of the work. This might be too simple to get you that A+ however.
    Last edited by Spidey1980; August 22nd, 2011 at 10:07 AM.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Location
    Brunei
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to programming

    I did that at first. I know it is a linear equation,the last exercise my teacher gave me was a quadratic equation.
    anyway,I would turn the equation in terms of t or s.
    but I am not sure how to assign 2 variables as they work in pairs.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Location
    Brunei
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to programming

    I am asked to list all the possible solutions.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Location
    Brunei
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to programming

    public class tablestools {
     
    	public static void main(String[] args)
    	  {
    	int s,t;
    	//100 = 3s + 4t;
     
    	s = (100-4t)/3;
    	t = (100-3s)/4;
     
     
     
    	  }
     
    }
    This is all I could think of right now.I know its garbage and super basic.
    -I am thinking of using a Math.random on one of the variables to generate the other but it may cause a infinite loop.(not sure how to counter that)
    -or by creating a class similar to "countdown" but i am not really familiar with that.

    fyi,I am using eclipse.
    I am sorry for the lack of info

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: New to programming

    You have two variables and only one equation. The way to solve this problem is to 'fix' one of the variables and find a value that works for the other. I guess you can derive a minimum and a maximum value for either stools or tables? Then you have to try each value in that range to see if there's a value for the other variable that satisfies the equation (and the integer constraint). To work through a range of values, you need a loop...

    I think you should post some code now. There are enough clues on this page for you to make a start.

    public class tableStool
    Class name with a lower case initial letter?
    Java Code Conventions

  8. #8
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: New to programming

    Quote Originally Posted by Sean4u View Post
    Class name with a lower case initial letter?
    Fixed, lol, I was trying not to spoon feed too much. My book has errors in the examples, a headache, but it makes you figure it out on your own.

    Quote Originally Posted by Cranky View Post
    This is all I could think of right now.I know its garbage and super basic.
    Put together the example I showed you. Put the do..while inside of the main, and put the rest of the code inside the do..while.

    Sorry if you saw it before I finished editing it. There is a complete example there now.
    Last edited by Spidey1980; August 22nd, 2011 at 10:09 AM.

  9. The Following User Says Thank You to Spidey1980 For This Useful Post:

    Cranky (August 22nd, 2011)

Similar Threads

  1. Hi everybody, im new to programming.
    By Sauliux in forum Member Introductions
    Replies: 1
    Last Post: March 5th, 2011, 11:06 AM
  2. [SOLVED] New to Programming
    By Delacratic in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2011, 01:45 PM
  3. New to programming
    By Milanocookies56@gmail.com in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2010, 09:55 AM
  4. Free Programming
    By freeprogramming in forum The Cafe
    Replies: 1
    Last Post: March 14th, 2010, 03:21 PM
  5. Please help me with this Java programming
    By simply_stunning79 in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 22nd, 2010, 06:48 PM