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."); } }