Solved

Default Int Values

Posted on 2003-11-26
17
237 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
ID: 9825677
You need a ";" after all the:

         Massey_Week3_InvoiceItem1 item3 = new Massey_Week3_InvoiceItem1() ;

lines
0
 
LVL 86

Expert Comment

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

Expert Comment

by:TimYates
ID: 9825687
lines 6, 17, etc

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

0
 

Author Comment

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

Expert Comment

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

Expert Comment

by:CEHJ
ID: 9825924
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
ID: 9826010
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
ID: 9826085
>>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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9826156
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
ID: 9826191
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
ID: 9826230
>>public class Massey_Week3_InvoiceItem1

remove 'public' from the above
0
 

Author Comment

by:MSAJM
ID: 9826337
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
ID: 9826362
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
ID: 9826439
Awesome!  Thanks a million, no a billion!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9826518
:-) 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
ID: 9826525
...although i see

setItemQuantity(1);

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

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

932 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

12 Experts available now in Live!

Get 1:1 Help Now