Results 1 to 8 of 8

Thread: Set and Get???

  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Set and Get???

    I have been working on a simple problem, but I am stuck. I am trying to learn parent and child classes and how they work. The program in broken into three classes; the DemoBook class that runs the various methods, the Book class that gathers information and displays it, and finally a child class of Book (called TextBook) that just gets one piece of data and then is suppossed to return that data back to Book. However, this is not working and I know I am missing something; I believe it has to do with Set and Get methods, but I am confused with how these work. Any help you can provide would be really greatly appreciated.


    Java Code:
    public class DemoBook
    {
      public static void main (String[] args)
      {
        Book aBook = new Book();
        Textbook aText = new Textbook();
        aBook.inputTitle();
        aBook.inputPages();
        aText.gradeLevel();
        aBook.showPages();
        
      }
    }
    
    import javax.swing.*;
    
    public class Book
    {
    
      private int pages;
      private String title;
      public int grade;
      
      public void showTitle()
      {
        JOptionPane.showMessageDialog (null, "Title of book: " + title);
      }
      
      public void inputTitle()
      {
        String titleString = new String(" ");
        titleString = JOptionPane.showInputDialog(null,
           "Enter the name of your book");
        title = titleString;
      }
      
      public void showPages()
      {
        JOptionPane.showMessageDialog (null, "The number of pages in " + title + " is " + pages +
      ". It is made for grade " + grade);
      }
      
      public void inputPages()
      {
        String pagesString = new String(" ");
        pagesString = JOptionPane.showInputDialog(null,
           "Enter the number of pages in your book");
        pages = Integer.parseInt(pagesString);
      }
       
    }
    
    import javax.swing.*;
    public class Textbook extends Book
    {
      //private int grade;
        
      public void gradeLevel()
      {
        String gradeString = new String(" ");
        gradeString = JOptionPane.showInputDialog(null,
           "Enter the grade level of your book");
        grade = Integer.parseInt(gradeString);
      }
    }

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Set and Get???

    Set and Get methods are used to set and get private variables from one class in another.

    Here's an example for an Integer x.

    Java Code:
    // getter method for x
    public int getX() {
        return x;
    }
    
    // setter method for x
    public void setX(int x) {
        this.x = x;
    }

  3. #3
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Set and Get???

    Ok so I have been playing with this but I am obviously missing something. I am not getting any errors, but I am still getting a 0 for the grade variable.

    Java Code:
    public class DemoBook
    {
      public static void main (String[] args)
      {
        Book aBook = new Book();
        Textbook aText = new Textbook();
        aBook.inputTitle();
        aBook.inputPages();
        aText.gradeLevel();
        aBook.showPages();
        
      }
    }
    Java Code:
    import javax.swing.*;
    
    public class Book
    {
    
      private int pages;
      private String title;
      public int grade;
      
      public void showTitle()
      {
        JOptionPane.showMessageDialog (null, "Title of book: " + title);
      }
      
      public void inputTitle()
      {
        String titleString = new String(" ");
        titleString = JOptionPane.showInputDialog(null,
           "Enter the name of your book");
        title = titleString;
      }
      
      public void showPages()
      {
        JOptionPane.showMessageDialog (null, "The number of pages in " + title + " is " + pages +
      ". It is made for grade " + grade);
      }
      
      public void inputPages()
      {
        String pagesString = new String(" ");
        pagesString = JOptionPane.showInputDialog(null,
           "Enter the number of pages in your book");
        pages = Integer.parseInt(pagesString);
      }
       
      public int getGrade()
      {
        return grade;
      }
       
    }
    Java Code:
    import javax.swing.*;
    public class Textbook extends Book
    {
        
      public void gradeLevel()
      {
        String gradeString = new String(" ");
        gradeString = JOptionPane.showInputDialog(null,
           "Enter the grade level of your book");
        grade = Integer.parseInt(gradeString);
      }
      
      public void setGrade (int grade)
      {
        this.grade = grade;
      }
    
    }

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Set and Get???

    Well your problem is that you have no assigned a value to the grade variable, so it is at its default value of 0.

  5. #5
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Set and Get???

    How do I assign a value if I want the user to input the value??

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Set and Get???

    First, your set and get for the grade should be in the same class. It makes little sense to have one in the subclass and one in the superclass. Second, to set the grade, you get the input and then call setGrade(grade).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Set and Get???

    You should not mix your book class with swing and joptionpane. Your book class should be very generic and not to have methods with user input inside.
    Your showpages or showtitle would just return a String and you will call this to display in JOptionPane in your main program.

  8. #8
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Set and Get???

    Ok. So I obviously have taken the wrong approach here. Thank you for your help. It looks like I need to go back to the drawing board.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •