Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Inheritance + Array

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Inheritance + Array

    Hi, I am newbie to Java programming language. Hope others can help me find out this problem. The problem occur at main class. Thank you in advance.

    p/s: sorry for broken english


    Superclass
    import java.util.*;
    import javax.swing.*;
     
    public class Publication
    {
        protected String id;
        protected String title;
        protected double price;
     
        public Publication(String a, String b, double c)
        {
            id=a;
            title=b;
            price=c;
        }
     
        public String getID()
        {
            return id;
        }
     
        public String getTitle()
        {
            return title;
        }
     
        public double getPrice()
        {
            return price;
        }
     
        public String disPublication()
        {
            return "ID: "+id+"\nTitle: "+title+"\nPrice: RM"+price;
        }
    }

    Book Class
    import java.util.*;
    import javax.swing.*;
     
    public class Book extends Publication
    {
        private int page;
        private double pricePage;
     
        //normal constructor
        public Book(String a, String b, double c, int d, double e)
        {
            super(a,b,c);
            page=d;
            pricePage=e;
        }
     
        public int getPage()
        {
            return page;
        }
     
        public double getPricePage()
        {
            return pricePage;
        }
     
     
        public double calcPrice()
        {
            return page*pricePage*price;
        }
    }

    Main Class
    import javax.swing.*;
    import java.util.*;
     
    public class BookApp
    {
        public static void main(String[] args)
        {
            Scanner sc=new Scanner(System.in);
            System.out.println("Please enter size of an array: ");
            int size=sc.nextInt();
     
            BookApp[] buku=new BookApp[size];
     
            for (int i=0; i<buku.length; i++)
            {
                System.out.print("Please enter ID: ");
                String id=sc.next();
                System.out.print("Please enter the title ");
                String title=sc.next();
                System.out.print("Please enter the Price: RM");
                double price=sc.nextDouble();
                System.out.print("Please enter number of pages: ");
                int page=sc.nextInt();
                System.out.print("Please enter price per page: RM");
                double pricePage=sc.nextDouble();
     
                Publication pub=new Publication(id, title, price);
                Book b=new Book(id, title, price, page, pricePage);
     
                buku[i]=new BookApp();
            }
     
            double total=0, avg=0;
            for (int i=0; i<buku.length; i++)
            {
                [COLOR="#FF0000"]total+=buku[i].calcPrice();[/COLOR]     //the error is here
            }
            avg=total/buku.length;
     
            int index=0;
            for (int i=0; i<buku.length; i++)
            {
                if (buku[i].calcPrice()>2000)
                    index++;
            }
            System.out.println("Books that has total cost more than RM2,000 is "+index);
     
            System.out.print("Enter ID: ");
            String userID=sc.next();
     
            boolean found=false;
            for (int i=0; i<buku.length; i++)
            {
                if (buku[i].id.equalsIgnoreCase(userID));
                {
                    System.out.println(disPublication()+" "+calcPrice());
                    found=true;
                }
            }   
            if (found=false)
                System.out.println("There's no publication information for input ID.");
        }   
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Inheritance + Array

    Welcome zarulizham

    What is the problem?
    If there is an error message, post the full text of the message here
    It is hard enough to figure out what functional code does at times, we can not guess what broken code was supposed to do, you have to tell us.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance + Array

    at the main class, got comment with "the error is here". Error is: cannot find symbol - method calcPrice()

    I think the way i call the method is wrong.


    this is the question.
    i stuck at c) iii.
    Create a superclass named Publication that contains the following information :

    Attributes :
    String id //publication id
    String title //publication title
    double price //publication price

    Methods:
    public Publication(String, String, double);
    public String getID();
    public String getTitle();
    public double getPrice();
    public String dispPublication();

    a) Complete the Publication class definition above.

    b) Create a new class Book that inherits from Publication class and has new attributes number of pages and price per page. Book has a method named calcPrice() that will calculate and return the total cost of publication for the book (total cost = number of pages * price per page * price of publication)

    c) Write Java application named BookApp to do the following :
    i. Declare a Book array and the size is entered by user.
    ii. Input the book data and store in the array
    iii. Find the average cost of all book objects input into an array.
    iv. Count how many books that has total cost more than RM2,000
    v. Display the publication information based on the ID entered by user, display an error message if ID is not found.
    thank you

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Inheritance + Array

    Hello.
    The problem is straightforward.
    If possible trace the program using a pen and pencil. You may be able to find out the error.
    To hint you just find out on which object you want to invoke the calcPrice()? Is that method available in the corresponding class?
    Did you create the objects of that particular class or some other classes?
    Are you storing the inputs from the keyboard into the main memory? If yes where?

    Syed.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Inheritance + Array

    Quote Originally Posted by zarulizham View Post
    at the main class, got comment with "the error is here". Error is: cannot find symbol - method calcPrice()
    Quote Originally Posted by jps View Post
    If there is an error message, post the full text of the message here
    ... ?

    How to help yourself get help.

  6. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Inheritance + Array

    you have to post the whole error message you get or else we will have to just guess until its correct

  7. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance + Array

    Quote Originally Posted by derekxec View Post
    you have to post the whole error message you get or else we will have to just guess until its correct
    Untitled-2.jpg
    you mean this error message?

  8. #8
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance + Array

    Quote Originally Posted by syedbhai View Post
    Are you storing the inputs from the keyboard into the main memory? If yes where?

    Syed.
    buku[i]=new BookApp();

    code for store input is correct or not? i need to store calcPrice() into buku[i] too right? how to do this. totally blind

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Inheritance + Array

    BookApp[] buku=new BookApp[size];
    This means that the variable named buku is an array of BookApp
    buku.calcPrice() means that BookApp has a method named calcPrice() ... which it does not
    The error message is trying to tell you it can not find a method named calcPrice within scope

    Either add a method named calcPrice to the BookApp class, use a method the BookApp class has, or perhaps the array was to be of Book and not of BookApp

Similar Threads

  1. Help with inheritance
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2012, 12:07 AM
  2. hi need help with inheritance
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 19th, 2012, 04:01 PM
  3. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  4. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  5. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM