This week's giveaway is in the Beginning Java forum.
We're giving away four copies of Bad Programming Practices 101 (e-book) and have Karl Beecher on-line!
See this thread for details.
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

copy with line numbers and help needed with classes  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is going to be difficult as I cannot find out how to copy with line numbers so I cannot point to the lines with problems.

in MainClass I get a bunch of error messages 'cannot resolve symbol' and in the line 'string s1 = Rational.toString()' I get 'non static method 'toString()' cannot be referenced from a static context'; Can anyone help me ? I am making the same mistake several times but cannot find the error. Also I thought there was a fairly straightforward method to transfer the line numbers. What is it? Thanks in anticipation.
 
Rancher
Posts: 3164
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I get a bunch of error messages


Please copy the full text of the error message and paste it here. It has important info about the error.
 
Saloon Keeper
Posts: 4362
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that you attempted to use Code tags to generate line numbers. You didn't do it quite right so I fixed it for you this time. To get Code tags to work properly, first highlight the entire block of code, and then click the Code button. When you think you've done it correctly, click the "Preview" button below the edit window, it should show you the code with line numbers in the Preview window.
 
Bartender
Posts: 616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have created an instance of Rational named "parts".
you should use that instance to call the non-static methods, so use
instead of
Also, none of your methods should be declared as static.
 
Robert Manley
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Copied java file into this window, highlighted and pressed "code" button.
Then clicked "preview".
No change. no line numbers.
What am I doing wrong?
Particularly annoying as I know I have done it correctly in the past.
 
Marshal
Posts: 59419
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robert Manley wrote:. . . What am I doing wrong? . . .

What should happen is that you get the input looking like this (abbreviated) before you push the preview or submit buttons:-
[code=java]public class MainClass {
// ...
public class Rational {

    private double num;
    private double denom;
    public double ratio;

// ...
    public static void negate(){
        double ratio = 0;
        ratio = - ratio;
    }
// ...[/code]

Because I used HTML tags for some of the characters, that code won't show up correctly, but that is what it should look like after you push the code button. When you push submit or preview, the line numbers should appear. You can probably see the effect by copying and pasting that output onto a new post.
Why are you using static fields? And static methods? That doesn't look object‑oriented to me.
Why have you called the fields num and denom? I thought it was numerator and denominator. Similarly for the methods.
Why are you using double fields in a Rational class? The definition of a rational number is one which can be defined as the ratio of two integers, so surely both numerator and denominator should be integers. If you create a ratio, that mustn't use integer arithmetic. Remember that ¾ should come out as 0.75 as a double, but if you use plain simple int arithmetic, it will evaluate to 0. Either cast one of the operands or try this sort of thing: 1.0 * numerator* / denominator. Remember that the usual problems about imprecision of floating‑point arithmetic will apply. Also, if you manage to get 0 as a denominator, that expression won't throw an exception but will evaluate to ±∞. I think the denominator should never be 0, however.
Why have you got ratio as a field? Why isn't it private?
What does the negate() method do? Look at it carefully.
 
Ranch Hand
Posts: 231
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robert Manley wrote:
Copied java file into this window, highlighted and pressed "code" button.
Then clicked "preview".
No change. no line numbers.
What am I doing wrong?
Particularly annoying as I know I have done it correctly in the past.



Hopefully, this offers a bit of clarification.

Based on my interpretation of what you wrote here, is that you pasted in your code, and then you pressed the code button.  Press the code button first. You should see the following line.

[code=java][\code]

Move the cursor between the ] and [ and then paste your code.  If this isn't the problem you are having, maybe give a little more detail such as enumerating each step you took.

Regards,

Robert
 
Saloon Keeper
Posts: 8988
168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also make sure that in your message options, BB Code isn't disabled.
 
Robert Manley
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK folks, I have sorted out all the obvious problems.
I know I have been repetitive in some cases with the 'println' statements, but that was just to get practice.
Are there any comments on the overall structure of the program which a beginner could learn from?
I am still unsure of the relative merits of 'static'.

Attached is my latest version.


 
Master Rancher
Posts: 2711
92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some remarks:

  • the most important property of a Rational is that the denominator must be unequal to zero. Add a check for this in the constructor.
  • don't change the ratio without changing the numerator and/or the denominator accordingly. Otherwise you will have an inconsistent Rational
  • but better, make your Rational immutable. If you want to negate a Rational, then simple return new Rational(-numerator, denominator)
  • the way you change a ratio will not always work. You should get a compiler error now.
  • and make the ratio final or private. As you have it now, anyone can change that value just like that



  • Edit: forget my fourth remark. You made some changes to your earlier code, which I missed.
     
    Campbell Ritchie
    Marshal
    Posts: 59419
    187
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Robert Manley wrote:. . . I know I have been repetitive in some cases with the 'println' statements . . .

    Lots of print statements are useful at the development stage to allow you to see what the program is going, so they are all right.

    I am still unsure of the relative merits of 'static'.

    If you haven't got a good explanation for making something static, I get suspicious if I see anything static.

    Attached is my latest version. . . .

    What appens if you print the ratio field? What do the printRational, invert, and divide methods do?
     
    Fred Kleinschmidt
    Bartender
    Posts: 616
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your variables num and denom are declared as int. This will give what might seem to be surprising results for ratio. For example, with num=10 and denom=40, ratio will be zero. The variables num and denom should be doubles.
     
    Sheriff
    Posts: 23594
    48
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Fred Kleinschmidt wrote:Your variables num and denom are declared as int. This will give what might seem to be surprising results for ratio. For example, with num=10 and denom=40, ratio will be zero. The variables num and denom should be doubles.



    I don't agree with all of that. Modelling a ratio as the quotient of two integers is a reasonable thing to do. However you're right that if you just divide the two integers, you're going to have a surprise. So to display the actual ratio you should divide the two integers converted to doubles.
     
    Campbell Ritchie
    Marshal
    Posts: 59419
    187
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought that problem had already been sorted.
     
    roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
    Why should you try IntelliJ IDEA ?
    https://coderanch.com/wiki/696337/IntelliJ-IDEA
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!