Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Program to output user entered info and calculate dosage based on species.  RSS feed

 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again all!  I am getting an error message and my code will not compile. I can not seem to figure out why. So any pointers in the right direction would be appreciated. The object of this program is to request user input, then return the input along with medication dosages based on the user's input. As I have mentioned before I am very new to Java. I am having some issues with the Java class I am in since there is really no instructor guidance. I  was told to purchase a book and given assignments that are not corresponding to the information in the book as we go. So I apologize for the funky code and thanks in advance for the pointers.
The error message that is showing up when I attempt to run my code is:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at pet.Pet.<init>(Pet.java:15)
at pet.PetDemo.main(PetDemo.java:155)
C:\Users\Violettwilight\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

And my code is as follows ( I should mention a large chunk of this code has been copied from source code from the course book-per professors instructions. I just had to make a few tweeks):

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rebecca,
The error is actually occurring in the Pet class. Can you show that class too?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems mistakenly wrote package name rather than PetDemo class name here, as Jeanne mentioned.
Also need to check the order of arguments here on line no 176 with respect to method declaration on line no 35
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always quote the details of the book, to avoid copyright problems, and also the page number, so we can find the source if we happen to have the book.
That Exception looks like what you get from NetBeans if you tell is to create a class with a number of methods. All methods have this structure (or similar):-That is useful for development; if you try a method which you have not yet completed, you suffer an Exception and you know about it. When you implement each method, you are supposed to delete the line about the Exception.

There is something not right about your drug methods. I think you should have a Drug class with name and mg per kilo fields instead of those methods. What is the 2.2 Is that some sort of conversion between lb and kg? Surely it is 2.205?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • I think it would be easier to find the error, if you follow general format like first variable declaration, constructors, setter & getter methods then main method.
  • You forgot to call ToString method here on line no 179.
  • It is good to use curly braces while using if-then, although they contain only one statement each.  Omitting them can make the code more brittle. If a second statement is later added to the "then" clause, a common mistake would be forgetting to add the newly required braces. The compiler cannot catch this sort of error; you'll just get the wrong results. One example here
  •  
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Agree about braces but not about the field speciesDog. You are going to create dreadful spaghetti code if you try all those fields. What about cats, and calculating the dose of morphine? You will have to think again about different dosages for different species.
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually you should not write things likeYou simply write,
    somethingElse(something());
    or, in this case,
    speciesDog = input.equalsIgnoreCase("y");
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Agree about braces but not about the field speciesDog. You are going to create dreadful spaghetti code if you try all those fields. What about cats, and calculating the dose of morphine? You will have to think again about different dosages for different species.
    Yes I think OP didn't consider other animals. It would be easy if we create a separate class named Pet which will have these fields String petName, int petAge, float petWeight and String petType.
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish Patil wrote:. . . It would be easy if we create a separate class named Pet which will have these fields String petName, int petAge, float petWeight and String petType.
    Maybe. But surely you are better off having an abstract Pet class and subclasses Dog, Cat, etc etc.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Maybe. But surely you are better off having an abstract Pet class and subclasses Dog, Cat, etc etc.
    Ohh I see, you mean declare common fields of Pets in Pet abstract class. Common behaviours of Pets comes in Pet abstract class like (Just assumed) sleeping(), drinkingWater() as defined methods of Pet abstract class whose implementation we know and methods (behaviour) which are type specific like makeNoise() where Dog barks and Cat meows so their implementation will be in respective classes i.e. type specific. Means when we know partial implementation then go for abstract class and when have no idea of implementation then go for interface, hope I'm correct?
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We have an FAQ about the differences between abstract classes and interfaces. I hope that will help.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I hope that will help
    Yes helped a lot, Wiki is really a great source of information, I better peruse them. Thank you 
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I was thinking you can overload the methods to calculate the dosages by type of Pet, but I now think I was mistaken and that may simply make the code really complicated.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!