• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Exam Flop - What went wrong?

 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm very much so new to programming, java especially. This is just my first semester of java and we've went as far as arrays. Anyway, today we had our final exam. Up until now I've done really well in the class, however I think between the time limit, test anxiety and maintenance workers being loud outside I couldn't focus well. My code wouldn't run, my objects weren't getting created so ultimately I had no way to really test anything out and was just shooting in the dark. Anyway, I saved my code to look over and even though it won't help me as far as my grade is concerned at this point, I still want to know what I did wrong and why this thing wouldn't work. Chalk it up as a learning experience and not make the same mistakes. So, for this assignment we had to create a class and a java file. The class is simply called Chair and it needed to have three overloaded constructors, one no-arg and the other two parameterized with the fields we were told to create. We were told to make a getCost method in the class that would use the field values to do some math and add 40 to the returned total if it didn't have a back, a setHeight method that would receive arguments for the chair height and a display method that would display all the fields and the cost. On the java ChairDemo we had to create three instances of the chair class and use each one with each of the constructors. and call the display method for each of the three chair objects to show their starting values. We also had to make a loop that would decrement chair height by 1 each iteration, changing the math done in the getCost methon in the class file and displaying the information for each iteration. We were also instructed to set the second chair object for use with one of the parameterized constructors up with the following arguments: hasBack set to true, whole number of legs set to 5, decimal value of height set to 31, name set to "Regular"

SO with that said, and coding totally blind because of unresolved errors, here's the mess I came up with. Oh also, note that first semester java students aren't allowed to use anything except the default windows notepad for coding. So fancy editors, nothing, so any 'painfully obvious errors' may not have been so obvious at the time because of that and we have to run everything through command line.

Chair.java



=================================================

ChairDemo.java
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nicole,

Welcome to CodeRanch!

Everybody has a learning phase and there are hurdles during it. Nothing unusual or wrong with that.

As you have access to your code - did you try to compile it and solve compilation issues?

My advice would be:
Take class which does not use any other class from your code (i.e. Chair class) and compile that only. Unless it compiles successfully, don't go for Demo class(es).

Pay very good attention to variable names, their datatypes, method invocation etc.

You may start with reading about syntax for method declaration/definition and method invocation. Let us know if you face any further doubts.

I hope this helps.

All The Best!
 
Nick Smithson
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't figure out what the compilation issues are. The only things we've used are notepad and command line. I know that it said there were errors, but I couldn't understand most of them. One error was regarding the decrement loop, because I used -1 and had no idea how else to do that. Then there were errors with the Chair objects, it couldn't find them or create them or something like that, which I didn't really understand. The problem is errors in general. I couldn't get just the Chair class to compile by itself because it's a class and so it doesn't have a main method or anything, which resulted in errors when trying to compile it by itself.
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicole Anon wrote: it doesn't have a main method or anything, which resulted in errors when trying to compile it by itself.


Classes do not need a main method to compile. That's not the issue.

When tackling compilation errors, focus on fixing the first one. Frequently, a single problem can cause a chain of failures that looks daunting. Once you fix the first error, compile again and address the first of any remaining errors. Don't try to tackle them all at once; that way lies madness!
 
Sheriff
Posts: 9021
656
Mac OS X Spring 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 Nicole.

1. Couple other advices. Since you don't have setter methods for all necessary chair attributes, which are critical in order to have a chair as a usable unit, consider removing no argument constructor, so you wouldn't let instantiate the chair without its number of legs and other critical attributes.

2. Have you been taught about keyword "this"? You don't need to think about different constructor parameter names and actual attribute names. You could simply have, just an example to visualise idea:

3. You do have method getCost, but actually it does slightly different. It calculates cost at first, then returns it. Think of option to decompose that method. So it would look similar to:

4. Also please fix your code indentation as it is inconsistent, it makes your code hard to read.

[addition]I just now noticed that you been told to create no-args constructor. Sorry, didn't notice that earlier. Ignore my 1st point.
 
Liutauras Vilda
Sheriff
Posts: 9021
656
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In no args constructor you don't need to have method call "display();"
as you're doing it from ChairDemo class

Even if your constructor not accepting any arguments (no-args), you could initialize all values to their defaults inside the constructor, it is a good practice to follow for readability. i.e.:
 
Marshal
Posts: 80874
506
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely you would initialise the legs to a default value rather than 0?There is another way to do that, using this as Liutauras suggested:-If you use this(...); it must be at the very beginning of a constructor.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicole Anon wrote:I can't figure out what the compilation issues are. The only things we've used are notepad and command line. I know that it said there were errors, but I couldn't understand most of them.


Then you should have stopped right there and talked to your tutor (or maybe another classmate) before you wrote another line of code.

One thing that most of us here would agree on: Beginners don't compile often enough.

My rules of thumb, for the next time you have to do this:
  • Compile roughly every ten lines you write (you have 68 in your Chair class alone).
  • Compile every time you finish writing a method or constructor, or something significant.
  • And those are ANDed, not ORed.

    The Java compiler is NOT forgiving, and it doesn't always do what we think it will. For example: simply missing out a brace ({ or }), or a semicolon (;), or putting it in the wrong place, can generate many errors for the same line. It is also case-sensitive, and NOT clever, so if you write 'hasback' somewhere instead of 'hasBack', it will tell you it doesn't recognise it.

    So, when you write classes, do it slowly, and compile them many times.

    Taking your Chair class, I would probably have done something like this:
    1. Written the declaration, viz:and compiled it. And by compiling, I mean compiling and changing until there are no more errors left.

    2. Added the fields viz:and compiled it again.

    2. Added a constructor:and compiled it again. And at this point I would get TWO errors at Line 11:
  • "The method setHeight(double) is undefined for the type" - Because I haven't defined setHeight() yet.
  • "Syntax error on token "double", delete this token" - Because the call is wrong. You don't include the type of an argument when you call a method.

  • But now I only have two errors to deal with, not 20.

    3. So, I remove that 'double' from Line 11, and compile it again (do you see a pattern here? ); and now I should only have 1 error left.

    4. So now I write the setHeight() method, so I can get rid of that last error. And I compile again...

    And so on, and so on.

    Do you see the point? By writing tiny amounts of code and compiling at every stage, you can correct errors immediately.
    You also don't get overwhelmed by great mountains of errors that you don't understand.

    HIH

    Winston
     
    Bartender
    Posts: 732
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Your constructor declares local variables that hide the instance variables:


    It should be:


    You have the same problem in the setters, such as setHeight().
     
    Campbell Ritchie
    Marshal
    Posts: 80874
    506
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Fred Kleinschmidt wrote:Your constructor declares local variables that hide the instance variables:. . .

    Lots of people make that mistake.
     
    Liutauras Vilda
    Sheriff
    Posts: 9021
    656
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Fred Kleinschmidt wrote:Your constructor declares local variables that hide the instance variables:. . .

    Lots of people make that mistake.

    Nicole, and using keyword this would prevent code from compiling (that is a good moment).

    If you ever would try to write:
    Without keyword this your code compiles, but it is even worse actually, as problem goes away silently without telling you that it exists. And some books advice that always use keyword this when your intention is to refer to an instance variable.
    reply
      Bookmark Topic Watch Topic
    • New Topic