Solved

Default Int Values

Posted on 2003-11-26
17
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

688 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