• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java linear equation problem

 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys! I am at the edge of finishing my math program. It's a program where you can choose what to do. For example calculate sphere volume, fractions, linear equations etc..
Well i have a major problem: My linear equation method isn't working at all. Well.. i don't know what to. I am kind of lost at this point. Have tried a bunch of things, but i think i screwed it up even more than before.
There is surely some more issues with the code, but I'm very new to this.. It isn't as obvious as for you guys. I would be very pleased and grateful if you could help me. If you see something else, tell me!
Here is the code!

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what line do you call your linear equation method?
 
William Nyqvister
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:At what line do you call your linear equation method?



I don't call it, cause i don't know how, and what to do..
The data that is returned from calcLine should be sent into printLine, that handles the output. But I don't know how I do it. The previous methods returns a value that i can output easily. But this confuses me..
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Nyqvister wrote:But this confuses me..


Me too .

Looking at your calcLine method, it's not immediately clear what it's supposed to do. We can probably be of some help if you describe, in words, what you are trying to accomplish.
 
William Nyqvister
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We can probably be of some help if you describe, in words



haha okey ill give the full details!

1: I'm supposed to create a method that takes integer values x, k and m. The method calculates and returns the value of y(x) = kx+m. The method should be called calcPoint, cause it calculates a point (x, y) of the line. If x=5, k=1 and m=2 the result of 1*5+2 is returned 7.

2: Now we create a method called calcLine, to calculate a line, all y of range from 1 to n, where n is the largest x value that is calculated. So if n is 5, x starts at 1 and ends at 5. The method also takes integer values of k and m, and uses calcPoint to calculate the value of y. The results are returned back to the main method in a integer array, where the position represents x. If n=5, k=1 and m=2 the following should be returned: {0, 3, 4, 5, 6, 7}. Note that position 1 (index 0) is not used when x starts at 1.

3: The last method printLine handles the printing form y(x) = kx + m. It receives the integer array with the y values and outputs it into the console. one point per line. If n = 5, k = 1 and m = 2 the output should be.

y(1) = 3
y(2) = 4
y(3) = 5
y(4) = 6
y(5) = 7

These three methods are tested by the user by inputting the values for n, k and m. The data that is returned from calcLine that sends it to printLine, which manages the output.

Well you should receive an Oscar if you understand xD
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, William, nice to see you around.

Probably first thing i'd go for, is - rename methods, so the method name would reveal actual its intention.
For example sumThis, maybe call it better as triangularNumber. And the base case probably should be 1, not 0. With ternary operators you could get 1 line method.
Lines 66, 67, 68. What these n, k and m mean? What these numbers are?

Probably you chose not quite right strategy of doing all this. You mentioned that one of your methods not working, so why you decided to go further without sorting it first.

[edit] Apart from calcLine method, the rest working as expected?
 
William Nyqvister
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lines 66, 67, 68. What these n, k and m mean? What these numbers are?



What do you mean by "lines 66, 67, 68"? You tell me! That is what the assignment says.. I guess the x is called n in the method to not confuse the user of the code with something else. I think the code works, but how to i get an output? :/

Apart from calcLine method, the rest working as expected?



Everything else is working! The "linear equation" method is the only one not working. Well that is the instructions for the task. I tried to make one of my own, cause i didn't really understand. But the output should be:

y(1) = 3
y(2) = 4
y(3) = 5
y(4) = 6
y(5) = 7

..after the user inputs kx+m to get the y. Well well, this one was to hard for me i guess
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you got wrong at least one more method after the calcLine. But let's don't take everything in one go - that is too much.

William Nyqvister wrote:2: Now we create a method called calcLine, to calculate a line, all y of range from 1 to n, where n is the largest x value that is calculated. So if n is 5, x starts at 1 and ends at 5. The method also takes integer values of k and m, and uses calcPoint to calculate the value of y. The results are returned back to the main method in a integer array, where the position represents x. If n=5, k=1 and m=2 the following should be returned: {0, 3, 4, 5, 6, 7}. Note that position 1 (index 0) is not used when x starts at 1.


Requirements of your method is above. And below is the code you have got.
Please read your requirements once again, and review the code you came up. Please state what comes to your mind, what have you missed and what have you interpreted in your way, even tho it was stated differently in the requirements.

I think you'll need to start over with this method and probably the next one.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Few more hints to give a start:

Why you decided to use array of strings?
Why you decided not to use array of integers?
Why your loop starts from the end of x, if requirements says "if n is 5, x starts at 1 and ends at 5"?
Why you are not using calcPoint method inside calcLine method, if you have been asked to do so?

William Nyqvister wrote:I guess the x is called n in the method to not confuse the user of the code with something else.

Probably you're mistaken here.

I would name your current parameter x as n. x and n are not the same.
It says: n is the largest x value that is needed to be calculated. i.e. if n = 4 then your x at some point will have value of 1, then of 2, then of 3 and finally value of 4.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic