Solved

Default Int Values

Posted on 2003-11-26
17
236 Views
Last Modified: 2010-03-31
public class Massey_Week3_CreateInvoice1
{
      public static void main(String[] args)
      {

            Massey_Week3_InvoiceItem1 item1 = new Massey_Week3_InvoiceItem1()
            item1.setItemNum(20239);
            item1.setItemName("Coors Light");
        item1.setItemQuantity(1);
        item1.setItemPrice(10.00f);
        item1.setTotalPrice(itemQuantity * itemPrice);
      }

      public static void main(String[] args)
      {

            Massey_Week3_InvoiceItem1 item2 = new Massey_Week3_InvoiceItem1()
            item2.setItemNum(20052);
            item2.setItemName("Coors Original");
            item2.setItemQuantity(1);
            item2.setItemPrice(11.00f);
            item2.setTotalPrice(itemQuantity * itemPrice);
      }

      public static void main(String[] args)
      {

            Massey_Week3_InvoiceItem1 item3 = new Massey_Week3_InvoiceItem1()
            item3.setItemNum(20151);
            item3.setItemName("Killians Irish Red");
            item3.setItemQuantity(1);
            item3.setItemPrice(12.00f);
            item3.setTotalPrice(itemQuantity * itemPrice);

      }

      public static void main(String[] args)
      {

            Massey_Week3_InvoiceItem1 item4 = new Massey_Week3_InvoiceItem1()
            item4.setItemNum(20024);
            item4.setItemName("Keystone Ice");
            item4.setItemQuantity(1);
            item4.setItemPrice(13.00f);
            iete4.setTotalPrice(itemQuantity * itemPrice);

      }

}
0
Comment
Question by:MSAJM
  • 9
  • 6
  • 2
17 Comments
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
You need a ";" after all the:

         Massey_Week3_InvoiceItem1 item3 = new Massey_Week3_InvoiceItem1() ;

lines
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can't have more than on emain method
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
lines 6, 17, etc

err...you also can't have more than one main method in the same class

0
 

Author Comment

by:MSAJM
Comment Utility
OK, obviouly I need to rewrite the whole program.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to parameterize the main method so you can do what you intend
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
And you really want a collection of objects to hold items. Perhaps you can tell me exactly what you want to achieve?
0
 

Author Comment

by:MSAJM
Comment Utility
Create 3 invoice items, (I have 4 possibly for extra credit:) using InvoiceItem.
Print out a header line
Print out the 3 (4) invoice item lines
Print out the total cost of the invoice

New and improved version, but still not compiling:

public class Massey_Week3_CreateInvoice1
{
      public static void main(String[] args)
      {

            Massey_Week3_InvoiceItem1 item1 = new Massey_Week3_InvoiceItem1("20239", "Coors Light", 10.00f);
            Massey_Week3_InvoiceItem2 item2 = new Massey_Week3_InvoiceItem2("20052", "Coors Original", 11.00f);
            Massey_Week3_InvoiceItem3 item3 = new Massey_Week3_InvoiceItem3("20151", "Killians Irish Red", 12.00f);
            Massey_Week3_InvoiceItem4 item4 = new Massey_Week3_InvoiceItem4("20024", "Keystone Ice", 13.00f);

            System.err.println("Item 1: " + item1.toString());
            System.err.println("Item 2: " + item2.toString());
        System.err.println("Item 3: " + item3.toString());
        System.err.println("Item 4: " + item4.toString());

        float totalPrice = item1.getItemPrice() + item2.getItemPrice() + item3.getItemPrice() + item4.getItemPrice();
            System.err.println("Total value: $" + totalPrice);
      }

}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Massey_Week3_InvoiceItem1

Is a class name. You only need one class, but four instances. Call it

Massey_Week3_InvoiceItem

and do

Massey_Week3_InvoiceItem item1 = new Massey_Week3_InvoiceItem("20239", "Coors Light", 10.00f);

...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Save this to file elsewhere as Massey_Week3_CreateInvoice1.java. It should get you started.



public class Massey_Week3_CreateInvoice1 {

      public static void main(String[] args) {

            Massey_Week3_InvoiceItem item1 = new Massey_Week3_InvoiceItem("20239", "Coors Light", 10.00f);
            Massey_Week3_InvoiceItem item2 = new Massey_Week3_InvoiceItem("20052", "Coors Original", 11.00f);
            Massey_Week3_InvoiceItem item3 = new Massey_Week3_InvoiceItem("20151", "Killians Irish Red", 12.00f);
            Massey_Week3_InvoiceItem item4 = new Massey_Week3_InvoiceItem("20024", "Keystone Ice", 13.00f);

            System.err.println("Item 1: " + item1.toString());
            System.err.println("Item 2: " + item2.toString());
            System.err.println("Item 3: " + item3.toString());
            System.err.println("Item 4: " + item4.toString());

            float totalPrice = item1.getItemPrice() + item2.getItemPrice() + item3.getItemPrice() + item4.getItemPrice();
            System.err.println("Total value: $" + totalPrice);
      }

}

class Massey_Week3_InvoiceItem {

      public Massey_Week3_InvoiceItem(String s1, String s2, float f1) {
            // unimplemented
      }

      public int getItemPrice() {
            //dummy
            return 100;
      }

      public String toString() {
            return "Dummy!";
      }

}
0
 

Author Comment

by:MSAJM
Comment Utility
OK, here we go.  I have almost figured it out, but I still have 1 error message.  I have to put the two programs together to get the desired results....duhhh!  OK, here is what I have now....

////////////////////////////////////////////////////////////////////////////
//
// File:     Massey_Week3_CreateInvoice1.java
//
// Project:  POS406
//
// Purpose:  Create Invoice
//
// Comments: Version 1 is what I would turn in if this were an assignment
//
// Author:   Amy Massey
//
///////////////////////////////////////////////////////////////////////////
public class Massey_Week3_InvoiceItem1
{

      private int itemNum = 0;
        private String itemName = "";
         private int itemQuantity = 1;
      private float itemPrice = 0.0f;
      private float totalPrice = 0.0f;


public void setItemNum(int valueIn)
{
    itemNum = valueIn;
    return;
         }
         public int getItemNum()
         {
    return itemNum;
}

public void setItemName(String valueIn)
{
     itemName = valueIn;
     return;
          }
          public String getItemName()
          {
     return itemName;
}

public void setItemQuantity(int valueIn)
{
     itemQuantity = valueIn;
     return;
          }
          public int getItemQuantity()
          {
     return itemQuantity;
}

public void setItemPrice(float valueIn)
{
       itemPrice = valueIn;
       return;
       }
       public float getItemPrice()
       {
       return itemPrice;
}

public void setTotalPrice(float valueIn)
{
      totalPrice = valueIn;
      return;
      }
      public float getTotalPrice()
      {
      return totalPrice;
}

public Massey_Week3_InvoiceItem1()
{
      setItemNum(0);
    setItemName("");
    setItemQuantity(1);
      setItemPrice(0.0f);
      setTotalPrice(0.0f);

    return;
}

public Massey_Week3_InvoiceItem1(int itemNumIn, String itemNameIn, int itemQuantityIn, float itemPriceIn, float totalPriceIn)
{
    setItemNum(itemNumIn);
    setItemName(itemNameIn);
    setItemQuantity(itemQuantityIn);
      setItemPrice(itemPriceIn);
      setTotalPrice(totalPriceIn);

       return;
      }

public class Massey_Week3_CreateInvoice1
{
      public static void main(String[] args)
      {

            Massey_Week3_InvoiceItem1 item1 = new Massey_Week3_InvoiceItem1("20239", "Coors Light", 1, 10.00f);
            Massey_Week3_InvoiceItem1 item2 = new Massey_Week3_InvoiceItem1("20052", "Coors Original", 1, 11.00f);
            Massey_Week3_InvoiceItem1 item3 = new Massey_Week3_InvoiceItem1("20151", "Killians Irish Red", 1, 12.00f);
            Massey_Week3_InvoiceItem1 item4 = new Massey_Week3_InvoiceItem1("20024", "Keystone Ice", 1, 13.00f);

            System.err.println("Item 1: " + item1.toString());
            System.err.println("Item 2: " + item2.toString());
        System.err.println("Item 3: " + item3.toString());
        System.err.println("Item 4: " + item4.toString());

        float totalPrice = item1.getItemPrice() + item2.getItemPrice() + item3.getItemPrice() + item4.getItemPrice();
            System.err.println("Total value: $" + totalPrice);
      }

}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>public class Massey_Week3_InvoiceItem1

remove 'public' from the above
0
 

Author Comment

by:MSAJM
Comment Utility
OK, now we have run into 5 more errors...
lines 102, 103, 104, 105...cannot resolve symbol...ahhhh! the ^ in under "new"

and line 99 inner classes cannot have static declarations.  any ideas on what I am doing wrong?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
You have return statements all over the place. They should not be there. The following compiles fine:



////////////////////////////////////////////////////////////////////////////
//
// File:     Massey_Week3_CreateInvoice1.java
//
// Project:  POS406
//
// Purpose:  Create Invoice
//
// Comments: Version 1 is what I would turn in if this were an assignment
//
// Author:   Amy Massey
//
///////////////////////////////////////////////////////////////////////////
class Massey_Week3_InvoiceItem1
{

     private int itemNum = 0;
       private String itemName = "";
        private int itemQuantity = 1;
     private float itemPrice = 0.0f;
     private float totalPrice = 0.0f;


public void setItemNum(int valueIn)
{
    itemNum = valueIn;
        }
        public int getItemNum()
        {
    return itemNum;
}

public void setItemName(String valueIn)
{
     itemName = valueIn;
         }
         public String getItemName()
         {
     return itemName;
}

public void setItemQuantity(int valueIn)
{
     itemQuantity = valueIn;
         }
         public int getItemQuantity()
         {
     return itemQuantity;
}

public void setItemPrice(float valueIn)
{
      itemPrice = valueIn;
      }
      public float getItemPrice()
      {
      return itemPrice;
}

public void setTotalPrice(float valueIn)
{
     totalPrice = valueIn;
     }
     public float getTotalPrice()
     {
     return totalPrice;
}

public Massey_Week3_InvoiceItem1()
{
     setItemNum(0);
    setItemName("");
    setItemQuantity(1);
     setItemPrice(0.0f);
     setTotalPrice(0.0f);

}

public Massey_Week3_InvoiceItem1(int itemNumIn, String itemNameIn, int itemQuantityIn, float itemPriceIn, float totalPriceIn)
{
    setItemNum(itemNumIn);
    setItemName(itemNameIn);
    setItemQuantity(itemQuantityIn);
     setItemPrice(itemPriceIn);
     setTotalPrice(totalPriceIn);

     }

public Massey_Week3_InvoiceItem1(String itemNameIn, String x, int y, float totalPriceIn)
{
  // NEEDS IMPLEMENTATION!
}


}


public class Massey_Week3_CreateInvoice1
{
     public static void main(String[] args)
     {

          Massey_Week3_InvoiceItem1 item1 = new Massey_Week3_InvoiceItem1("20239", "Coors Light", 1, 10.00f);
          Massey_Week3_InvoiceItem1 item2 = new Massey_Week3_InvoiceItem1("20052", "Coors Original", 1, 11.00f);
          Massey_Week3_InvoiceItem1 item3 = new Massey_Week3_InvoiceItem1("20151", "Killians Irish Red", 1, 12.00f);
          Massey_Week3_InvoiceItem1 item4 = new Massey_Week3_InvoiceItem1("20024", "Keystone Ice", 1, 13.00f);

          System.err.println("Item 1: " + item1.toString());
          System.err.println("Item 2: " + item2.toString());
        System.err.println("Item 3: " + item3.toString());
        System.err.println("Item 4: " + item4.toString());

        float totalPrice = item1.getItemPrice() + item2.getItemPrice() + item3.getItemPrice() + item4.getItemPrice();
           System.err.println("Total value: $" + totalPrice);
     }

}
0
 

Author Comment

by:MSAJM
Comment Utility
Awesome!  Thanks a million, no a billion!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-) Don't forget to implement the constructor. The following:

>>
public Massey_Week3_InvoiceItem1()
{
     setItemNum(0);
    setItemName("");
    setItemQuantity(1);
     setItemPrice(0.0f);
     setTotalPrice(0.0f);

}
>>

is redundant as

a. Java supplies the empty constructor
b. Java zeroes out values automatically

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
...although i see

setItemQuantity(1);

so that does alter things if you want to set that
0
 

Author Comment

by:MSAJM
Comment Utility
I will set that back to 0, and thanks for all your help with this!  I really, really appreciate it!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now