super class function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmarcrum
    New Member
    • Oct 2007
    • 105

    super class function

    Hi everyone!

    I'm using a super class (DVD.java) that handles another class (EnhancedDVD.ja va). I want to pass the "details" of the DVD into the super class DVD.java. The super class contains the Title, RegionCode, Format, and Length of a dummy DVD...let's say..."Forrest Gump." But I can't figure out how to do this and I have 4 errors going on when I compile the two classes. I have a driver that runs the System.out.prin tln's. But I think it might be handling the classes wrongly as well. Any help? What am I doing wrong?

    I want to take this one step at a time. First I'd like to just print out the "details" of the DVD, not worrying about the toString method in the DVD.java class.

    But anyways...here' s my code for the two classes and the driver. I'll bold where my errors exist, they exist in the ENHANCEDDVD.jav a class...i have 3 errors

    DVD.java class
    --------------------------------------------------------------------------------------------------------
    Code:
    /*
     * File: DVD.java
     * Author:
     * Vers: 1.0.0.0, 1/31/2008, jdm - Initial coding
     * Vers: 1.0.0.1, 2/19/2008, jdm - modified for TUI
     * Vers. 1.0.0.2, 2/28/2008, jdm - modified for super class
     * Desc: This program models a DVD in several ways.
     */
    
    /**
    * Beginning of the DVD class
    */
    public class DVD
    {
        public static final int REGION_FREE = 0; // Final variable for the DVD region_free code
        public static final int NTSC_FORMAT = 1; // Final variable for the DVD NTSC_format
        public static final int PAL_FORMAT = 2; // Final variable for the DVD PAL_format
        public static final int DVD_SECTOR_SIZE = 2048; // Final variable for the DVD sector size
        public static final int DATA_RATE = 4096 * 1000; // Final variable for the DVD sector size in bytes
        
        public static int serialNumberDVD = 0; // int static variable for the DVD serial number
        public static int region; // int variable for the DVD region
        public static int format; // int variable for the DVD format
        public static String title; // String variable for the DVD title
        public static double length = 0.0; // double variable for the DVD length in minutes
        
        private int sectorNumber = 0; // int variable for the DVD length in minutes
        
        /**
        * Constructor: builds the DVD object
        */
        public DVD(String title, int region, int format, double length)
        {      
          this.title = title; // initialization of title
          this.region = region; // initialization of region
          this.format = format; // initialization of format
          this.length = length; // initialization of length
            
          serialNumberDVD = serialNumberDVD + 1; // increments the serial number
        }    
        
        //Queries:-----------------------------------------
        
        /**
        * Query: getClassAuthor
        */ 
        public static String getClassAuthor() // returns name of class’ author (my name)
        {  
          return " my name " + "\n"; // returns my name
        }
        
        /**
        * Query: returns the DVD sector number
        */
        public long getSectorNumber(double minutes)
        {
          long bytesToGoThrough = (long)(minutes * 60 * DATA_RATE) / 8; // converts to bits
          long sectorNumber = bytesToGoThrough / DVD_SECTOR_SIZE + 1024; // computes the sector size of the DVD
          return sectorNumber; // returns the sector number
        }
        
        /**
        * Query: returns the DVD serial number
        */
        public static int getSerialNumberDVD()
        {
          return serialNumberDVD; // returns the serial number
        }
    
        /**
        * Query: returns the DVD title
        */
        public static String getTitle()
        {
          if (region >= 0 && region <= 8) // conditional AND statement
            {
              return title; // return DVD title
            }
            else
            {
              return "Bad Region Code"; // return Bad Region Code
            }
        }
        
        /**
        * Query: returns the DVD region
        */
        public int getRegion()
        {
          return region; // returns the DVD region
        }
        
        /**
        * Query: returns the DVD format
        */
        public int getFormat()
        {
          return format; // returns the DVD format
        }
        
        /**
        * Query: returns the DVD length
        */
        public double getLength()
        {
          return length; // returns the DVD length
        }
        
        //Commands:--------------------------------------------
        
        /**
        * Command: toString
        */
        public String toString() // returns a text representation of all the data pertaining to a given 
                                 // DVD: "serial number, title, region, format, length."
        {                             
          // return of the full DVD printable object - formatted as a form
          // (properly formatted for display, and without the quotes).
          return "\n" + "Serial Number: " + getSerialNumberDVD() + "\n" + 
            "Title: " + getTitle() + "\n" + 
            "Region Code: " + getRegion() + "\n" + 
            "Format: " + getFormat() + "\n" + 
            "Length: " + getLength();
        }
    }
    EnhancedDVD.jav a
    ------------------------------------------------------------------------------------------------------
    Code:
    /*
     * File: EnhancedDVD.java
     * Author:
     * Vers: 1.0.0.0, 3/4/2008, jdm - Initial coding
     * Desc: This class gathers "details" for a DVD
     */
    
    /**
     * Beginning of the EnhancedDVD class.
     */ 
    [U][I][B]public class EnhancedDVD implements DVD[/B][/I][/U]
    {
      public String dvdDetails = ""; // string DVD details
      /**
       * Constructor: EnhancedDVD constructor that passes in the initial DVD details (dvdDetails).
       */ 
      public EnhancedDVD(String title, int region, int format, double length, String dvdDetails)
      { 
        [U][I][B]super(title, region, format, length); // passes the dvdDetails into the EnhancedDVD constructor[/B][/I][/U]    this.dvdDetails = dvdDetails; // assign this dvdDetails to dvdDetails
      } 
        
      // --------------- Beginning of queries ---------------------------
        
      /**
       * Query: getdvdDetails
       */
      public String getDetails()
      {
        return dvdDetails; // return of the DVD's details
      }
        
      // --------------- Beginning of commands ---------------------------
        
      /**
       * Command: setDVDDetails
       */
      public static void setDetails(String DetailsOfDVD)
      {
        [U][I][B]this.dvdDetails = DetailsofDVD; // sets the DVD's details[/B][/I][/U]
      }
      
      public String toString() // returns a text representation of all the data pertaining to a given 
                               // DVD's details: stars of the movie, movie rating, genre, basic plot, etc., etc., 
      {                             
        // return of the details of the DVD printable object - formatted as a form
        // (properly formatted for display, and without the quotes).
        return "\n" + "Details: " + getDetails();
      }
    }
    Driver class
    ----------------------------------------------------------------------------------------------------------
    Code:
    /*
     * File: Driver5.java
     * Author:  
     * Vers: 1.0.0.0, 2/28/2008, jdm - Initial coding
     * Desc: This is a driver for the program
     */
    
    public class Driver5 {
    
        public static void main(String[] args) {
            
            System.out.println("Spring 2008 P5 by " + DVD.getClassAuthor()); // print my name as author
                     
            EnhancedDVD newDVD = new EnhancedDVD("Forrest Gump", 1, DVD.NTSC_FORMAT, 200.); // creates the newDVD object
            EnhancedDVD.setDetails("Stars: Tom Hanks, Robin White" + "\n" +
                              "Rating: PG-13" + "\n" + 
                              "Genre: Drama" + "\n" + 
                              "Plot: The title character leads viewers through an accidental" + "\n" + 
                              "travelogue of American social history from the early 1960s" + "\n" +
                              "through the present in this revisionist fable."); 
            
            //System.out.println(newdvd); // print dvd
            
            //System.out.println("Last Sectors:"); // print DVD sectors
            //System.out.println("For \"" + dvd.getTitle() + "\": " + 
            //                     dvd.getSectorNumber(dvd.getLength())); // print dvd1 sectors    
            
            System.out.println(newDVD); // print the details of the DVD
        }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    I didn't look at your code too closely because as soon as I started reading, I saw that you have some basic misconceptions. Do you understand "static"?

    What is the difference between a static field and a non-static (instance) field?

    How does one determine which to use?

    There are basic questions and you not should write another line of code until you can answer these questions without hesitation.

    Comment

    • jmarcrum
      New Member
      • Oct 2007
      • 105

      #3
      Originally posted by BigDaddyLH
      I didn't look at your code too closely because as soon as I started reading, I saw that you have some basic misconceptions. Do you understand "static"?

      What is the difference between a static field and a non-static (instance) field?

      How does one determine which to use?

      There are basic questions and you not should write another line of code until you can answer these questions without hesitation.
      don't you use static if you want to KEEP something from changing? The things I have made static are that way for a reason. If i delete the static assignment I get MORE errors. Also, for other concepts...use public and private if you want to be able to reference the instance variables via their respective class from other classes

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by jmarcrum
        don't you use static if you want to KEEP something from changing?
        No, I'm afraid that's got absolutely nothing to do with the keyword static. You should review the language basics. Here is a page from Sun's tutorial that talks about static:


        Originally posted by jmarcrum
        The things I have made static are that way for a reason. If i delete the static assignment I get MORE errors.
        I'm afraid that's not a reason. That's voodoo. Just because one error leads you to make more errors doesn't make any of the errors, uh, not an error.

        What you need to do is understand static then use it where it's needed and don't use it where it's wrong to use it.

        My rule of thumb is to almost never use it, except for three spots:

        1. You program's main method is required to be static
        2. Constants can be static:

        [CODE=Java]public static final int DALMATIONS = 101;[/CODE]

        3. Utility classes like java.lang.Math can have static methods (sqrt, sin, cos, ...)

        Comment

        • jmarcrum
          New Member
          • Oct 2007
          • 105

          #5
          Originally posted by BigDaddyLH
          No, I'm afraid that's got absolutely nothing to do with the keyword static. You should review the language basics. Here is a page from Sun's tutorial that talks about static:




          I'm afraid that's not a reason. That's voodoo. Just because one error leads you to make more errors doesn't make any of the errors, uh, not an error.

          What you need to do is understand static then use it where it's needed and don't use it where it's wrong to use it.

          My rule of thumb is to almost never use it, except for three spots:

          1. You program's main method is required to be static
          2. Constants can be static:

          [CODE=Java]public static final int DALMATIONS = 101;[/CODE]

          3. Utility classes like java.lang.Math can have static methods (sqrt, sin, cos, ...)
          I appreciate you pointing me in the right direction BigDaddyLH. i understand where i need to be going. I've actually changed my code a good bit since my last post now that I am in the right mindset! I changed my code to now look like this...however, I'm getting 1 other error...you are absolutely right about errors.

          The error is in my EnhancedDVD.jav a class on line 33 (this.dvdDetail s = DetailsOfDVD; // sets the DVD's details
          ). Could you please point in in the right direction one more time? IT IS an error about a static variable....als o, i need to print out the DVD''s toString method as well...but how would i do that?

          I'm such a newbie at this stuff!!!

          I would greatly appreciate it BigDaddyLH!!!!! !!

          DVD.java
          -----------------------------------------------------------------------------------------------------
          [CODE=Java]/**
          * Beginning of the DVD class
          */
          public class DVD
          {
          public static final int REGION_FREE = 0; // Final variable for the DVD region_free code
          public static final int NTSC_FORMAT = 1; // Final variable for the DVD NTSC_format
          public static final int PAL_FORMAT = 2; // Final variable for the DVD PAL_format
          public static final int DVD_SECTOR_SIZE = 2048; // Final variable for the DVD sector size
          public static final int DATA_RATE = 4096 * 1000; // Final variable for the DVD sector size in bytes

          public static int serialNumber = 0; // int static variable for the DVD serialNumber

          private String title; // String variable for the DVD title
          private int region; // int variable for the DVD region
          private int format; // int variable for the DVD format
          private double length = 0.0; // double variable for the DVD length in minutes
          private int sectorNumber = 0; // int variable for the DVD length in minutes

          /**
          * Constructor: builds the DVD object
          */
          public DVD(String title, int region, int format, double length)
          {
          this.title = title; // initialization of title
          this.region = region; // initialization of region
          this.format = format; // initialization of format
          this.length = length; // initialization of length

          serialNumber = serialNumber + 1; // increments the serial number
          }

          //Queries:-----------------------------------------

          /**
          * Query: getClassAuthor
          */
          public static String getClassAuthor( ) // returns name of class’ author (my name)
          {
          return "my name" + "\n"; // returns my name
          }

          /**
          * Query: returns the DVD sector number
          */
          public long getSectorNumber (double minutes)
          {
          long bytesToGoThroug h = (long)(minutes * 60 * DATA_RATE) / 8; // converts to bits
          long sectorNumber = bytesToGoThroug h / DVD_SECTOR_SIZE + 1024; // computes the sector size of the DVD
          return sectorNumber; // returns the sector number
          }

          /**
          * Query: returns the DVD serial number
          */
          public static int getSerialNumber ()
          {
          return serialNumber; // returns the serial number
          }

          /**
          * Query: returns the DVD title
          */
          public String getTitle()
          {
          return title; // returns the title
          }

          /**
          * Query: returns the DVD region
          */
          public int getRegion()
          {
          return region; // returns the region
          }

          /**
          * Query: returns the DVD format
          */
          public int getFormat()
          {
          return format; // returns the format
          }

          /**
          * Query: returns the DVD length
          */
          public double getLength()
          {
          return length; // returns the length
          }

          //Commands:--------------------------------------------

          /**
          * Command: toString
          */
          public String toString() // returns a text representation of all the data pertaining to a given DVD: "serial number, title, region, format, length."
          {
          // return of the full DVD printable object - formatted as a form
          // (properly formatted for display, and without the quotes).
          return "Serial Number: " + getSerialNumber () + "\n" +
          "Title: " + getTitle() + "\n" +
          "Region: " + getRegion() + "\n" +
          "Format: " + getFormat() + "\n" +
          "Length: " + getLength() + "\n";
          }
          }[/CODE]

          EnhancedDVD.jav a
          ---------------------------------------------------------------------------------------------------------------
          [CODE=Java]/**
          * Beginning of the EnhancedDVD class.
          */
          public class EnhancedDVD extends DVD
          {
          public String dvdDetails = ""; // string DVD details
          /**
          * Constructor: EnhancedDVD constructor that passes in the initial DVD details (dvdDetails).
          */
          public EnhancedDVD(Str ing title, int region, int format, double length)
          {
          super(title, region, format, length); // passes the DVD class instance variables into the EnhancedDVD constructor
          this.dvdDetails = dvdDetails; // assign this dvdDetails to dvdDetails
          }

          // --------------- Beginning of queries ---------------------------

          /**
          * Query: getdvdDetails
          */
          public String getDetails()
          {
          return dvdDetails; // return of the DVD's details
          }

          // --------------- Beginning of commands ---------------------------

          /**
          * Command: setDVDDetails
          */
          public static void setDetails(Stri ng DetailsOfDVD)
          {
          this.dvdDetails = DetailsOfDVD; // sets the DVD's details
          File: C:\Desktop\Enha ncedDVD\Enhance dDVD.java [line: 33]
          Error: non-static variable this cannot be referenced from a static context
          }

          public String toString() // returns a text representation of all the data pertaining to a given
          // DVD's details: stars of the movie, movie rating, genre, basic plot, etc., etc.,
          {
          // return of the details of the DVD printable object - formatted as a form
          // (properly formatted for display, and without the quotes).
          return "Details: " + getDetails();
          }
          }[/CODE]

          Driver5.java
          ---------------------------------------------------------------------------------------------------------------
          [CODE=Java]/**
          * Beginning of the Driver5 class
          */
          public class Driver5 {

          public static void main(String[] args) {

          System.out.prin tln("Spring 2008 P5 by " + DVD.getClassAut hor()); // print my name as author

          EnhancedDVD newDVD = new EnhancedDVD("Fo rrest Gump", 1, DVD.NTSC_FORMAT , 200.); // creates the EnhancedDVD object
          newDVD.setDetai ls("Stars: Tom Hanks, Robin White" + "\n" +
          "Rating: PG-13" + "\n" +
          "Genre: Drama" + "\n" +
          "Plot: The title character leads viewers through an accidental" + "\n" +
          "travelogue of American social history from the early 1960s" + "\n" +
          "through the present in this revisionist fable.");

          System.out.prin tln(newDVD); // print the details of the DVD
          }
          }[/CODE]

          Comment

          • qintao1987
            New Member
            • Feb 2008
            • 2

            #6
            maybe the key reason is the implements misused;

            change the imlements to extends

            Comment

            • jmarcrum
              New Member
              • Oct 2007
              • 105

              #7
              OK!!!, I got it working....ever ything looks good. Many thanks to everyone who posted!! I would never make it through java alone!!! All i had to do was remove the "static" type like BigDaddyLH said. Here's the final working solution....

              DVD.java class
              ------------------------------------------------------------------------------------------------------------
              [CODE=Java]/**
              * Beginning of the DVD class
              */
              public class DVD
              {
              public static final int REGION_FREE = 0; // Final variable for the DVD region_free code
              public static final int NTSC_FORMAT = 1; // Final variable for the DVD NTSC_format
              public static final int PAL_FORMAT = 2; // Final variable for the DVD PAL_format
              public static final int DVD_SECTOR_SIZE = 2048; // Final variable for the DVD sector size
              public static final int DATA_RATE = 4096 * 1000; // Final variable for the DVD sector size in bytes

              public static int serialNumber = 0; // int static variable for the DVD serialNumber

              public String title; // String variable for the DVD title
              public int region; // int variable for the DVD region
              public int format; // int variable for the DVD format
              public double length = 0.0; // double variable for the DVD length in minutes
              public int sectorNumber = 0; // int variable for the DVD length in minutes

              /**
              * Constructor: builds the DVD object
              */
              public DVD(String title, int region, int format, double length)
              {
              this.title = title; // initialization of title
              this.region = region; // initialization of region
              this.format = format; // initialization of format
              this.length = length; // initialization of length

              serialNumber = serialNumber + 1; // increments the serial number
              }

              //Queries:-----------------------------------------

              /**
              * Query: getClassAuthor
              */
              public static String getClassAuthor( ) // returns name of class’ author (my name)
              {
              return "Joseph D. Marcrum, III" + "\n"; // returns my name
              }

              /**
              * Query: returns the DVD sector number
              */
              public static long getSectorNumber (double minutes)
              {
              long bytesToGoThroug h = (long)(minutes * 60 * DATA_RATE) / 8; // converts to bits
              long sectorNumber = bytesToGoThroug h / DVD_SECTOR_SIZE + 1024; // computes the sector size of the DVD
              return sectorNumber; // returns the sector number
              }

              /**
              * Query: returns the DVD serial number
              */
              public static int getSerialNumber ()
              {
              return serialNumber; // returns the serial number
              }

              /**
              * Query: returns the DVD title
              */
              public String getTitle()
              {
              return title; // returns the title
              }

              /**
              * Query: returns the DVD region
              */
              public int getRegion()
              {
              return region; // returns the region
              }

              /**
              * Query: returns the DVD format
              */
              public int getFormat()
              {
              return format; // returns the format
              }

              /**
              * Query: returns the DVD length
              */
              public double getLength()
              {
              return length; // returns the length
              }

              //Commands:--------------------------------------------

              /**
              * Command: toString
              */
              public String toString() // returns a text representation of all the data pertaining to a given DVD: "serial number, title, region, format, length."
              {
              // return of the full DVD printable object - formatted as a form
              // (properly formatted for display, and without the quotes).
              return "Serial Number: " + getSerialNumber () + "\n" +
              "Title: " + getTitle() + "\n" +
              "Region: " + getRegion() + "\n" +
              "Format: " + getFormat() + "\n" +
              "Length: " + getLength() + "\n";
              }
              }[/CODE]

              EnhancedDVD.jav a class
              -----------------------------------------------------------------------------------------------------------------
              [CODE=Java]/**
              * Beginning of the EnhancedDVD class.
              */
              public class EnhancedDVD extends DVD
              {
              public String dvdDetails = ""; // string DVD details
              /**
              * Constructor: EnhancedDVD constructor that passes in the initial DVD details (dvdDetails).
              */
              public EnhancedDVD(Str ing title, int region, int format, double length)
              {
              super(title, region, format, length); // passes the DVD class instance variables into the EnhancedDVD constructor
              this.dvdDetails = dvdDetails; // assign this dvdDetails to dvdDetails
              }

              // --------------- Beginning of queries ---------------------------

              /**
              * Query: getdvdDetails
              */
              public String getDetails()
              {
              return dvdDetails; // return of the DVD's details
              }

              // --------------- Beginning of commands ---------------------------

              /**
              * Command: setDVDDetails
              */
              public void setDetails(Stri ng DetailsOfDVD)
              {
              this.dvdDetails = DetailsOfDVD; // sets the DVD's details
              }

              public String toString() // returns a text representation of all the data pertaining to a given
              // DVD's details: stars of the movie, movie rating, genre, basic plot, etc., etc.,
              {
              // return of the details of the DVD printable object - formatted as a form
              // (properly formatted for display, and without the quotes).
              return "Title: " + title + "\n" +
              "Region: " + region + "\n" +
              "Format: " + format + "\n" +
              "Length: " + length + "\n" + "\n" +
              getDetails() + "\n";
              }
              }[/CODE]

              Driver5.java class
              -------------------------------------------------------------------------------------------------------------
              [CODE=Java]/**
              * Beginning of the Driver5 class
              */
              public class Driver5 {

              public static void main(String[] args) {

              System.out.prin tln("Spring 2008 P5 by " + DVD.getClassAut hor()); // print my name as author

              EnhancedDVD newDVD = new EnhancedDVD("Fo rrest Gump", 1, DVD.NTSC_FORMAT , 200.); // creates the EnhancedDVD object
              newDVD.setDetai ls("Stars: Tom Hanks, Robin White" + "\n" +
              "Rating: PG-13" + "\n" +
              "Genre: Drama" + "\n" +
              "Plot: The title character leads viewers through an accidental" + "\n" +
              "travelogue of American social history from the early 1960s" + "\n" +
              "through the present in this revisionist fable.");

              System.out.prin tln(newDVD); // print the details of the DVD

              System.out.prin tln( "Last Sectors:");
              System.out.prin tln( "For \"" + newDVD.title + "\": " +
              DVD.getSectorNu mber(newDVD.len gth) ); // print newdvd sectors
              }
              }[/CODE]

              Comment

              Working...