subclass confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • porky008
    New Member
    • Oct 2006
    • 20

    #1

    subclass confusion

    I need to make a subclass for the following code. I was planning on using the extends keyword but the instructor says to use =new keyword. I do not understand the = new keyword. Can someone please explain the difference and possibly provide an example based on the following code.
    Code:
     import java.util.Scanner;
    
    public class inventorysupplys2 { // start of class
    
    
        public static void main(String[] args) { // start of program
    
            Scanner input = new Scanner( System.in );
             int c;
            System.out.printf( "\n\nHow many new movies do you want to enter? ");
            c = input.nextInt();
            input.nextLine(); 
            inventorysupplystock2[] supply = new inventorysupplystock2[c];
    
    
            String name;              // for the name
            double number;            // for the item number
            double quantity;          // number of units in stock
            double price;             // price per item
            double value = 0;         // value of that item
            double totalValue = 0;    // value of whole inventory
    
            int a = 0;
            while (a < c){ // start of the loop getting the info for how ever many you are putting in
    
                supply[a] = new inventorysupplystock2();
    
                System.out.printf( "What is the name of the movie being entered: ");
                name = input.nextLine();
                supply[a].setName(name);
    
                System.out.printf( "What will be this movie's item number: ");
                number = input.nextDouble();
                input.nextLine(); 
                supply[a].setNumber(number);
    
                System.out.printf( "How many are you entering into stock: ");
                quantity = input.nextDouble();
                input.nextLine(); 
                supply[a].setquantity(quantity);
    
                System.out.printf( "How much did this movie cost: ");
                price = input.nextDouble();
                input.nextLine(); 
                supply[a].setPrice(price);
    
                value = supply[a].getquantity() * supply[a].getPrice();
    
                supply[a].setValue(value);
    
                supply[a].setCount(a);
    
                a++;
    
            } // end of loop getting info
    
            java.util.Arrays.sort(supply); //this sorts the stuff
            System.out.printf( "Item number  Name   Quantity  Price  Value \n");
    
            a = 0;
            while (a < c){ // total value of inventory
            supply[a].displayItems();
    
            totalValue += supply[a].getValue();
    
            a++;
            } 
    
     
    
            // total value
            System.out.printf( "The total value of the inventory is: %.2f", totalValue );
        } // end of program
    
    } // end of class
    
    
    ************************************************************
    
    public class inventorysupplystock2 implements Comparable{ 
             protected  int count;
             protected  int quantity;
             protected  String productName;
             protected  double itemNumber;
             protected  double quantityNumber;
             protected  double unitPrice;
             protected  double unitValue;
    
    
            public inventorysupplystock2() {
    
        }
    
        public int compareTo(Object o) {
        return productName.compareTo(((inventorysupplystock2) o).getName());
        }
    
        public void displayItems(){
            System.out.printf( "%-2s           %-2s       %-2s      %.2f     %.2f\n",
                    itemNumber, productName,
                    quantityNumber, unitPrice, unitValue);
        }
        // total value per movie
        public void setValue(double value){
            unitValue = value;
    
        }
    
        public double getValue(){
    
              return unitValue;
        }
    
        public void setCount( int a ){
            count = a;
        }
    
     
        // movie name
        public void setName( String name ) {
    
            productName = name;
        }
        public String getName() {
    
            return productName;
        }
        // item number
        public void setNumber( double number ) {
    
            itemNumber = number;
        }
        public double getNumber() {
    
            return itemNumber;
        }
        // quantity of that movie
        public void setquantity( double quantity ) {
    
            quantityNumber = quantity;
        }
        public double getquantity() {
    
            return quantityNumber;
        }
        // price for that movie
        public void setPrice( double price ) {
    
            unitPrice = price;
        }
        public double getPrice(){
    
            return unitPrice;
        }
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by porky008
    I need to make a subclass for the following code. I was planning on using the extends keyword but the instructor says to use =new keyword. I do not understand the = new keyword. Can someone please explain the difference and possibly provide an example based on the following code.
    Code:
     import java.util.Scanner;
    
    public class inventorysupplys2 { // start of class
    
    
        public static void main(String[] args) { // start of program
    
            Scanner input = new Scanner( System.in );
             int c;
            System.out.printf( "\n\nHow many new movies do you want to enter? ");
            c = input.nextInt();
            input.nextLine(); 
            inventorysupplystock2[] supply = new inventorysupplystock2[c];
    
    
            String name;              // for the name
            double number;            // for the item number
            double quantity;          // number of units in stock
            double price;             // price per item
            double value = 0;         // value of that item
            double totalValue = 0;    // value of whole inventory
    
            int a = 0;
            while (a < c){ // start of the loop getting the info for how ever many you are putting in
    
                supply[a] = new inventorysupplystock2();
    
                System.out.printf( "What is the name of the movie being entered: ");
                name = input.nextLine();
                supply[a].setName(name);
    
                System.out.printf( "What will be this movie's item number: ");
                number = input.nextDouble();
                input.nextLine(); 
                supply[a].setNumber(number);
    
                System.out.printf( "How many are you entering into stock: ");
                quantity = input.nextDouble();
                input.nextLine(); 
                supply[a].setquantity(quantity);
    
                System.out.printf( "How much did this movie cost: ");
                price = input.nextDouble();
                input.nextLine(); 
                supply[a].setPrice(price);
    
                value = supply[a].getquantity() * supply[a].getPrice();
    
                supply[a].setValue(value);
    
                supply[a].setCount(a);
    
                a++;
    
            } // end of loop getting info
    
            java.util.Arrays.sort(supply); //this sorts the stuff
            System.out.printf( "Item number  Name   Quantity  Price  Value \n");
    
            a = 0;
            while (a < c){ // total value of inventory
            supply[a].displayItems();
    
            totalValue += supply[a].getValue();
    
            a++;
            } 
    
     
    
            // total value
            System.out.printf( "The total value of the inventory is: %.2f", totalValue );
        } // end of program
    
    } // end of class
    
    
    ************************************************************
    
    public class inventorysupplystock2 implements Comparable{ 
             protected  int count;
             protected  int quantity;
             protected  String productName;
             protected  double itemNumber;
             protected  double quantityNumber;
             protected  double unitPrice;
             protected  double unitValue;
    
    
            public inventorysupplystock2() {
    
        }
    
        public int compareTo(Object o) {
        return productName.compareTo(((inventorysupplystock2) o).getName());
        }
    
        public void displayItems(){
            System.out.printf( "%-2s           %-2s       %-2s      %.2f     %.2f\n",
                    itemNumber, productName,
                    quantityNumber, unitPrice, unitValue);
        }
        // total value per movie
        public void setValue(double value){
            unitValue = value;
    
        }
    
        public double getValue(){
    
              return unitValue;
        }
    
        public void setCount( int a ){
            count = a;
        }
    
     
        // movie name
        public void setName( String name ) {
    
            productName = name;
        }
        public String getName() {
    
            return productName;
        }
        // item number
        public void setNumber( double number ) {
    
            itemNumber = number;
        }
        public double getNumber() {
    
            return itemNumber;
        }
        // quantity of that movie
        public void setquantity( double quantity ) {
    
            quantityNumber = quantity;
        }
        public double getquantity() {
    
            return quantityNumber;
        }
        // price for that movie
        public void setPrice( double price ) {
    
            unitPrice = price;
        }
        public double getPrice(){
    
            return unitPrice;
        }
    }
    Are you sure that your professor was referring to the code of the subclass when you were told to use 'new'? Generally the 'new' keyword is used when declaring an object, such as in your code above:

    Code:
            Scanner input = new Scanner( System.in );
    This code will create a Scanner on the heap. However, if you are writing a subclass, your inclination was correct (though I would suggest this article ' http://www.javaworld.c om/javaworld/jw-08-2003/jw-0801-toolbox.html' if you are considering 'extends' and instead suggest 'implements' as inventorysupply 2 does to Comparable.)

    Code:
    public class ExampleClass extends inventorysupply2 /*or Comparable, if that's what you want here */{
    
    // methods, variables, and other things in here
    
    }
    Was your professor possibly telling you to use 'new' when instantiating your subclass in your final program?

    Comment

    • porky008
      New Member
      • Oct 2006
      • 20

      #3
      Originally posted by sicarie
      Are you sure that your professor was referring to the code of the subclass when you were told to use 'new'? Generally the 'new' keyword is used when declaring an object, such as in your code above:

      Code:
              Scanner input = new Scanner( System.in );
      This code will create a Scanner on the heap. However, if you are writing a subclass, your inclination was correct (though I would suggest this article ' http://www.javaworld.c om/javaworld/jw-08-2003/jw-0801-toolbox.html' if you are considering 'extends' and instead suggest 'implements' as inventorysupply 2 does to Comparable.)

      Code:
      public class ExampleClass extends inventorysupply2 /*or Comparable, if that's what you want here */{
      
      // methods, variables, and other things in here
      
      }
      Was your professor possibly telling you to use 'new' when instantiating your subclass in your final program?
      all i know u he said "You do not need to use the extends keyword. The file with the static void main will call the otherfile.java into existence via the =New keyword." so i am very ocnfused now about how to add this subclass

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by porky008
        all i know u he said "You do not need to use the extends keyword. The file with the static void main will call the otherfile.java into existence via the =New keyword." so i am very ocnfused now about how to add this subclass
        1) You do not make a subclass of code but of a class
        2)You cannot make a subclass without using extends
        3)Perhaps you are supposed to come up with a different type of inventorysupply stock2 class and replace the current one with the new one in the main method. (All which have nothing to do with subclassing)

        Comment

        • porky008
          New Member
          • Oct 2006
          • 20

          #5
          Originally posted by r035198x
          1) You do not make a subclass of code but of a class
          2)You cannot make a subclass without using extends
          3)Perhaps you are supposed to come up with a different type of inventorysupply stock2 class and replace the current one with the new one in the main method. (All which have nothing to do with subclassing)
          This is what I have to do modify the inventory program by creating a subclass of product class that uses one additional unique feature of the product you choose (for the DVDs subclass you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a %5 restocking fee to the value of the inventory of that product. This is what I have so far can I get some help with a few errors?
          Code:
          public class inventorysubclass extends inventorysupplys2{ 
          
               protected String Title;
               protected string Genre;
               protected double value;
               protected double restockingfee;
           
               // four-argument contructor
               public Title (String Title, String Genre, String Genre, double value, double restockingfee)
               {
          	//implicit call to DVD contructor occurs here
          	Title = Title;
          	Genre = Genre
          	setValue(value); //validate value of DVD
          	setRestockingFee(rate); //validate and store restocking fee
               } // end four-argument MoviteTitle contructor
           
               // set title
               public void setTitle(String first)
               {
          	Title=Title;
               } //end method setTitle
           
               // return title
               public String getTitle()
               {
          	return Title;
               } //end method getTitle
           
               // set Genre
               public void getGenre (String Genre)
               {
          	Genre=Genre;
               }
          	return Genre;
               } // end method getGenre
           
               //set value of Dvd
               public void setValue (double value)
               {
          	return Value;
               } // end method getValue
           
               // set restocking fee
               public void setRestockingFee(double restockingfee)
               {
          	resstockingFee = (value * 5%)
               } // end method setRestockingFee
           
               // return restocking fee
               public double getRestockingFee()
               {
          	return restockingFee;
               } // end method getRestockingFee
           
               // caluculate value + restocking fee
               public double total()
               {
          	return restockingFee * Value;
               } // end method total

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by porky008
            This is what I have to do modify the inventory program by creating a subclass of product class that uses one additional unique feature of the product you choose (for the DVDs subclass you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a %5 restocking fee to the value of the inventory of that product. This is what I have so far can I get some help with a few errors?
            Code:
            public class inventorysubclass extends inventorysupplys2{ 
             
            protected String Title;
            protected string Genre;
            protected double value;
            protected double restockingfee;
             
            // four-argument contructor
            public Title (String Title, String Genre, String Genre, double value, double restockingfee)
            {
            	//implicit call to DVD contructor occurs here
            	Title = Title;
            	Genre = Genre
            	setValue(value); //validate value of DVD
            	setRestockingFee(rate); //validate and store restocking fee
            } // end four-argument MoviteTitle contructor
             
            // set title
            public void setTitle(String first)
            {
            	Title=Title;
            } //end method setTitle
             
            // return title
            public String getTitle()
            {
            	return Title;
            } //end method getTitle
             
            // set Genre
            public void getGenre (String Genre)
            {
            	Genre=Genre;
            }
            	return Genre;
            } // end method getGenre
             
            //set value of Dvd
            public void setValue (double value)
            {
            	return Value;
            } // end method getValue
             
            // set restocking fee
            public void setRestockingFee(double restockingfee)
            {
            	resstockingFee = (value * 5%)
            } // end method setRestockingFee
             
            // return restocking fee
            public double getRestockingFee()
            {
            	return restockingFee;
            } // end method getRestockingFee
             
            // caluculate value + restocking fee
            public double total()
            {
            	return restockingFee * Value;
            } // end method total
            Yes you can get help about the errors if you tell us what the errors are.

            Comment

            • porky008
              New Member
              • Oct 2006
              • 20

              #7
              Originally posted by r035198x
              Yes you can get help about the errors if you tell us what the errors are.
              I got the errors worked out but I still can not get the restocking fee working.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by porky008
                I got the errors worked out but I still can not get the restocking fee working.
                Now that you realise that a void method should not return anything, explain then what you mean by can not get the restocking fee working.

                Comment

                • porky008
                  New Member
                  • Oct 2006
                  • 20

                  #9
                  Originally posted by r035198x
                  Now that you realise that a void method should not return anything, explain then what you mean by can not get the restocking fee working.
                  I need to add a restocking fee of 5% to this inventory program. I can not seem to get the coding for it right.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by porky008
                    I need to add a restocking fee of 5% to this inventory program. I can not seem to get the coding for it right.
                    Where are you not getting it right?
                    If you have the complete program you are testing with, post the code, tell us what you are getting as output and what you want to have as output, then you will get your solution quicker.

                    Comment

                    • porky008
                      New Member
                      • Oct 2006
                      • 20

                      #11
                      Originally posted by r035198x
                      Where are you not getting it right?
                      If you have the complete program you are testing with, post the code, tell us what you are getting as output and what you want to have as output, then you will get your solution quicker.
                      this is what i have and i can not seem to get the re-stocking fee to work. can i please get some help fixing this. thanks
                      Code:
                      public class inventorysupplystock2 implements Comparable{
                               protected  int count;
                               protected  int quantity;
                               protected  String productName;
                               protected  String productGenre;
                               protected  double itemNumber;
                               protected  double quantityNumber;
                               protected  double unitPrice;
                               protected  double unitValue;
                               protected  double restockingFee;
                      
                              public inventorysupplystock2() {
                      
                          }
                      
                          public int compareTo(Object o) {
                          return productName.compareTo(((inventorysupplystock2) o).getName());
                          }
                      
                          public void displayItems(){
                              System.out.printf( "%-2s           %-2s          %-2s         %-2s        %.2f        %.2f       %.2f\n",
                                      itemNumber, productGenre, productName,
                                      quantityNumber, unitPrice, unitValue, restockingFee);
                          }
                          // total value per movie
                          public void setValue(double value){
                              unitValue = value;
                      
                          }
                      
                          public double getValue(){
                      
                                return unitValue;
                          }
                      
                       public void setRestockingFee(double restockingfee)
                           {
                      	restockingFee = (restockingfee * 1.05);
                           } // end method setRestockingFee
                      
                           // return restocking fee
                           public double getRestockingFee()
                           {
                      	return restockingFee;
                           } // end method getRestockingFee
                      
                           // caluculate value + restocking fee
                           public double total()
                           {
                      	return restockingFee * 1.05;
                           } // end method total
                      
                          public void setCount( int a ){
                              count = a;
                          }
                      
                      
                          // movie name
                          public void setName( String name ) {
                      
                              productName = name;
                          }
                          public String getName() {
                      
                              return productName;
                             }
                              // movie genre
                          public void setGenre( String genre ) {
                      
                              productGenre = genre;
                          }
                          public String getGenre() {
                      
                              return productGenre;
                             }
                      
                          // item number
                          public void setNumber( double number ) {
                      
                              itemNumber = number;
                          }
                          public double getNumber() {
                      
                              return itemNumber;
                          }
                          // quantity of that movie
                          public void setquantity( double quantity ) {
                      
                              quantityNumber = quantity;
                          }
                          public double getquantity() {
                      
                              return quantityNumber;
                          }
                          // price for that movie
                          public void setPrice( double price ) {
                      
                              unitPrice = price;
                          }
                          public double getPrice(){
                      
                              return unitPrice;
                          }
                      }
                      ***************************************
                      import java.util.Scanner;
                      
                      public class inventorysupplys2 { // start of class
                      
                      
                          public static void main(String[] args) { // start of program
                      
                              Scanner input = new Scanner( System.in );
                              int c;
                              System.out.printf( "\n\nHow many new movies do you want to enter? ");
                              c = input.nextInt();
                              input.nextLine();
                              inventorysupplystock2[] supply = new inventorysupplystock2[c];
                      
                      
                              String name;              // for the name
                              String genre;             // this should be the subclass genre
                              double number;            // for the item number
                              double quantity;          // number of units in stock
                              double fee;
                              double price;             // price per item
                              double value = 0;         // value of that item
                              double totalValue = 0;    // value of whole inventory
                              double restockingFee = 0; // this should be fo rthe restocking fee
                              int a = 0;
                              int b = 0;
                              while (a < c){ // start of the loop getting the info for how ever many you are putting in
                      
                                  supply[a] = new inventorysupplystock2();
                                  supply[b] = new inventorysupplystock2();
                      
                                  System.out.printf( "What is the name of the movie being entered: ");
                                  name = input.nextLine();
                                  supply[a].setName(name);
                      
                                  System.out.printf( "What is the genre of the movie being entered: ");
                                  genre = input.nextLine();
                                  supply[b].setGenre(genre);
                      
                                  System.out.printf( "What will be this movie's item number: ");
                                  number = input.nextDouble();
                                  input.nextLine();
                                  supply[a].setNumber(number);
                      
                                  System.out.printf( "How many are you entering into stock: ");
                                  quantity = input.nextDouble();
                                  input.nextLine();
                                  supply[a].setquantity(quantity);
                      
                                  System.out.printf( "How much did this movie cost: ");
                                  price = input.nextDouble();
                                  input.nextLine();
                                  supply[a].setPrice(price);
                      
                                  System.out.printf( "How much is the restocking fee: ");
                                  fee = input.nextDouble();
                                  input.nextLine();
                      
                      
                                  value = supply[a].getquantity() * supply[a].getPrice();
                      
                                  supply[a].setValue(value);
                      
                                  supply[a].setCount(a);
                                  // i could not get the restocking fee to work but i do know its
                                  // resstockingFee = (value * 1.05) i just didnt know how to work it in here
                                  a++;
                      
                              } // end of loop getting info
                      
                              java.util.Arrays.sort(supply); //this sorts the stuff
                              System.out.printf( "Item number  Genre   Name   Quantity  Price  Value restockingFee\n");
                      
                              a = 0;
                              while (a < c){ // total value of inventory
                              supply[a].displayItems();
                      
                              totalValue += supply[a].getValue();
                      
                              a++;
                              }
                      
                      
                      
                              // total value
                              System.out.printf( "The total value of the inventory is: %.2f", totalValue );
                          } // end of program
                      
                      } // end of class

                      Comment

                      Working...