Results 1 to 7 of 7
  1. #1
    KRUKUSA is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Java programming help!

    Write (modify) the following 2 programs. When you have completed the programs, attach the Java source files ( GradeBook.java, GradeBookTest.java, Invoice.java, InvoiceTest.java ) in TalonNet to assignment A04.

    Program 1 - GradeBook class modifications

    Modify class GradeBook ( Fig 3.10 ) as follows: Here is a link to the source code for Figure 3-10 --> Figure_3.10 zip file
    Include a second String instance variable (field) that represents the name of the instructor for the course.
    Provide a set method to change the instructor's name and a get method to retrieve it.
    Modify the constructor to specify two parameters - one for the course name and one for the course instructor.
    Modify method displayMessage such that it first outputs the welcome message and course name, then outputs "This course is presented by: " followed by the instructor's name.
    Modify the GradeBookTest class to demonstrate the use of ALL the methods in the class.

    Program 2 - Invoice class

    Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables (fields) - a part number (type String), a part description (type String), a quantity of the item being purchased (type int), and a price for the item (type double).

    Your class should have a constructor that initializes the four instance variables. Provide set and a get methods for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value.

    Write a test application named InvoiceTest to demonstrate the use of ALL the methods in the class.


    Instruction: Here is my code

    public

    class GradeBook
    {
    private String professorSurname;
    private String courseName;


    public GradeBook( String name )
    {
    courseName = name;
    professorSurname = surname;
    }

    public void setCourseName( String name )
    {
    courseName = name;
    }

    public void setProfessorSurname ( String surname )
    {
    professorSurname = surname;
    }

    public String getCourseName()
    {
    return courseName;
    }

    public String getProfessorSurname()
    {
    return professorSurname;
    }
    public void displayMessage()
    {
    System.out.printf( "Welcome to the grade book for\n%s!\n",
    getCourseName() );
    }

    }

    /************************************************** ************************
    * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and *
    * Pearson Education, Inc. All Rights Reserved. *
    * *
    * DISCLAIMER: The authors and publisher of this book have used their *
    * best efforts in preparing the book. These efforts include the *
    * development, research, and testing of the theories and programs *
    * to determine their effectiveness. The authors and publisher make *
    * no warranty of any kind, expressed or implied, with regard to these *
    * programs or to the documentation contained in these books. The authors *
    * and publisher shall not be liable in any event for incidental or *
    * consequential damages in connection with, or arising out of, the *
    * furnishing, performance, or use of these programs. *
    ************************************************** ***********************/

    public

    class GradeBookTest
    {

    public static void main( String args[] )
    {

    GradeBook gradeBook1 = new GradeBook(
    "CS101 Introduction to Java Programming" );
    GradeBook gradeBook2 = new GradeBook(
    "CS102 Data Structures in Java" );


    System.out.printf( "gradeBook1 course name is: %s\n",
    gradeBook1.getCourseName() );
    System.out.printf( "gradeBook2 course name is: %s\n",
    gradeBook2.getCourseName() );
    System.out.printf("This gradeBook1 professor name is:y %s\n",
    gradeBook1.getProfessorSurname () );
    System.out.printf ("This gradeBook2 professor name is: %s\n",
    gradeBook2.getProfessorSurname () );

    }

    }

    Please help me: Thanks in advance


    The error I'm getting is
    GradeBook.java:12: cannot find symbol
    symbol : variable surname
    location: class GradeBook
    professorSurname = surname;
    ^
    1 error

    I know I have to define the variable but where and how? First time modifying something.

  2. #2
    Baloch is offline GuRu
    Join Date
    Feb 2012
    Location
    Quetta
    Posts
    3
    Rep Power
    0

    Default Re: Java programming help!

    hi dude ,
    First you should look at error and understand what and in which line error exists. The error says that there is an error in GradeBook.java on line number 12. As it is great quotation "Understanding the solution is half of the problem" . There is a problem in your constructor.

    public GradeBook( String name )
    {
    courseName = name;
    professorSurname = surname;
    }

    you are passing only one argument to your constructor of type String but you need two arguments. the second one of type string and surname in your case. So your constructor should look like this

    public GradeBook( String name,String surname )//and you should pass two arguments when calling this constructor
    {
    courseName = name;
    professorSurname = surname;
    }

    I hope it helps.
    Last edited by Baloch; 02-28-2012 at 10:20 AM.

  3. #3
    KRUKUSA is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java programming help!

    Thank you now I am getting the error of.


    GradeBookTest.java:9: cannot find symbol
    symbol : constructor GradeBook(java.lang.String)
    location: class GradeBook
    GradeBook gradeBook1 = new GradeBook(
    ^
    GradeBookTest.java:11: cannot find symbol
    symbol : constructor GradeBook(java.lang.String)
    location: class GradeBook
    GradeBook gradeBook2 = new GradeBook(
    ^
    2 errors

    Do you know why that is?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Java programming help!

    Quote Originally Posted by Baloch View Post
    So your constructor should look like this

    public GradeBook( String name,String Name )
    Please don't post advice that contravenes every Java coding convention. Variable names start with a lowercase letter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    KRUKUSA is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java programming help!

    Quote Originally Posted by DarrylBurke View Post
    Please don't post advice that contravenes every Java coding convention. Variable names start with a lowercase letter.

    db
    well that doesn't really help me :(

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java programming help!

    "cannot find symbol" compiler messages mean what they say: you have used something (typically a variable, method or constructor) and the compiler cannot figure out what variable, method or constructor you mean.

    One obvious reason for this can be typos. Saying arrIndex when you meant arrayIndex. Capitalisation is a special case of this: you are obliged to be absolutely consistent about the case of the words you use for variables, constructors and methods. DarrylBurke's suggestion is very helpful in achieving this.

    When methods and constructors are involved there may be no spelling mistakes, but there can still be a missing symbol because the number or type of arguments is wrong.

    When the compiler reports a missing symbol, it is comparing the text with everything that has been declared. If there is no matching declaration, you get the missing symbol message. In the case of methods and constructors if you use the wrong number or type of arguments the usage won't match the declaration and you'll be told you have a missing symbol.

    Java Code:
        // (A) foo is declared as an int
    int foo;  
        // (B) setFoo() is declared as a void method taking an int argument
        // (C) bar is declared as an int
    void setFoo(int bar) { 
        // ...
    }
    With those declarations in mind it is easy to spot the missing symbols

    Java Code:
    int foo;  
    void setFoo(int bar) { 
        foo = bar2; // bad! bar2 is missing (not declared)
        f00 = bar; // bad! f00 is missing (typo)
    }
    void someMethod() {
        setFoo(42, 666); // bad! (wrong number of arguments)
        SetFoo(42); // bad! (incorrect capitalisation)
    }
    You would be well advised to understand *both* lots of compiler messages in the light of the need for the text you use (what the compiler calls a "symbol") to match exactly the declaration of that symbol.

    The error I'm getting is
    GradeBook.java:12: cannot find symbol
    symbol : variable surname
    location: class GradeBook
    professorSurname = surname;
    ^
    So, where was surname declared in the code of your OP? (Hint: it wasn't. You just started using it within the constructor.)

    GradeBookTest.java:9: cannot find symbol
    symbol : constructor GradeBook(java.lang.String)
    location: class GradeBook
    GradeBook gradeBook1 = new GradeBook(
    ^
    Where was a GradeBook constructor taking a single String argument declared? (Hint: it wasn't. You just started using it on line 9, but the constructor's declaration had recently been changed.)

    -----

    The problem in each case is a mismatch between symbol usage and declaration. And the solution in each case begins by finding the declaration. Java programs are very formal affairs and every symbol must be properly introduced by a declaration. Then you check for typos (including Java coding violations) and for the number and type of any arguments involved.
    Last edited by pbrockway2; 02-28-2012 at 03:10 AM.

  7. #7
    Baloch is offline GuRu
    Join Date
    Feb 2012
    Location
    Quetta
    Posts
    3
    Rep Power
    0

    Default Re: Java programming help!

    Quote Originally Posted by DarrylBurke View Post
    Please don't post advice that contravenes every Java coding convention. Variable names start with a lowercase letter.

    db
    Sorry it was just a typo.

    @KARUKUSA I have edited my previous post. I hope you won't get error and look for typo mistakes as pbrockway2 said.
    Last edited by Baloch; 02-28-2012 at 10:25 AM. Reason: Typo error

Similar Threads

  1. Replies: 21
    Last Post: 06-01-2009, 12:52 PM
  2. Replies: 1
    Last Post: 05-31-2009, 10:57 PM
  3. Replies: 1
    Last Post: 05-31-2009, 06:48 PM
  4. Replies: 1
    Last Post: 05-31-2009, 02:17 PM
  5. Replies: 16
    Last Post: 05-28-2009, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •