Witam Was, jest to mój pierwszy post.
Raczkuję z Javą, wspomagam się kursami online i książką pt. "Programowanie praktyczne od podstaw" Krzysztofa Barteczko.
Publikacja powyższa prezentuje pewien przykład kodu, który jest rozbity i nie został w książce zaprezentowany w całości. Być może jest to powodem, że coś przeoczyłem i program przepisany przeze mnie nie działa jak powinien. Nie łapię jeszcze słownictwa, pewne elementy mylą mi się, więc zamieszczam skany 4 stron publikacji z urywkami kodu. Ponadto zamieszczam dwie części kodu zapisanego przeze mnie (kod pierwszy zawiera jedną klasę, kod drugi zawiera drugą klasę).
Kod pierwszy:
public class Publication {
private String title;
private String publisher;
private int year;
private String ident;
private double price;
private int quantity;
public Publication (String t, String pb, int y,String i, double pr, int q) {
title = t;
publisher = pb;
year = y;
ident = i;
price = pr;
quantity = q;
}
public String getTitle() {
return title;
}
public String getPublisher() {
return publisher;
}
public int getYear() {
return year;
}
public String getIdent() {
return ident;
}
public double getPrice() {
return price;
}
public void setPrice(double pr) {
price = pr;
}
public int getQuantity() {
return quantity;
}
public void buy(int n) {
quantity += n;
}
public void sell(int n) {
quantity -= n;
}
}
Kod drugi, który ma testować działanie programu z kodu pierwszego
public class PubTest {
public static void main(String[] args) {
Publication b = new Publication ("Psy", "Dog&Sons", 2002, "ISBN6789", 21.0, 0);
int n = 10;
b.buy(n);
double koszt = n * b.getPrice();
System.out.println("Na zakup " + n + " publikacji:");
System.out.println(b.getTitle());
System.out.println(b.getPublisher());
System.out.println(b.getYear());
System.out.println(b.getIdent());
System.out.println("---------------\nwydano: " + koszt);
b.sell(4);
System.out.println("Po sprzedaży zostało" + b.getQuantity() + " pozycji");
}
}
Po próbie kompilowania programu błędy wskazują na element konstruktora, jako do poprawki. Mają następującą treść:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Publication(String, String, int, String, double, int) is undefined
The method buy(int) is undefined for the type Publication
The method getPrice() is undefined for the type Publication
The method getTitle() is undefined for the type Publication
The method getPublisher() is undefined for the type Publication
The method getYear() is undefined for the type Publication
The method getIdent() is undefined for the type Publication
The method sell(int) is undefined for the type Publication
The method getQuantity() is undefined for the type Publication
at PubTest.main(PubTest.java:6)
Nie jestem sobie jednak w stanie poradzić z rozwiązaniem. Metody prób i błędów zawodzą. Będę bardzo wdzięczny za pomoc i mam nadzieję, że jako nowy użytkownik forum nie popełniłem błędu i post nie zostanie usunięty.
Pozdrawiam Michał