# New to programming

• August 22nd, 2011, 07:45 AM
Cranky
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 :)

edit:i posted on the wrong section :), you can still help me out here
• August 22nd, 2011, 08:00 AM
Sean4u
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.
• August 22nd, 2011, 08:56 AM
Spidey1980
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:
Code java:

```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.

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

Code java:

```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:

Code java:

```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:

Code java:

`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.
• August 22nd, 2011, 09:00 AM
Cranky
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.
• August 22nd, 2011, 09:03 AM
Cranky
Re: New to programming
I am asked to list all the possible solutions.
• August 22nd, 2011, 09:26 AM
Cranky
Re: New to programming
Code Java:

```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
• August 22nd, 2011, 09:27 AM
Sean4u
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.

Quote:

public class tableStool
Class name with a lower case initial letter?
Java Code Conventions
• August 22nd, 2011, 09:41 AM
Spidey1980
Re: New to programming
Quote:

Originally Posted by Sean4u
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
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.