• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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.
 
Master Rancher
Posts: 3276
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: 4732
52
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: 620
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: 60060
188
  • 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: 9213
177
  • 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.


 
Rancher
Posts: 2813
96
  • 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: 60060
    188
    • 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: 620
    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: 23687
    50
    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: 60060
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought that problem had already been sorted.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!