Witam
Jestem studentem informatyki i ostatnio zacząłem naukę Javy. Ostatnio na zajęciach dostałem takie zadanie:
Należy zmodyfikować wcześniej napisany program z listy 5 w taki sposób aby wszystkie pola klasy Samochód były dostępne maksymalnie z poziomu pakietu. Dodatkowo dodać zmienną Lp. która będzie dostępna tylko w klasie AutoKomis.To jest kod tego programu z listy 5:
package zadania_lab5;
import java.util.Date;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Samochód {
//deklaracja zmiennych.
private String theCarBrand;
private String model;
private double capacity;
private String type;
private int engine;
private double yerOfProduction;
private String dateOfFirstRegistration;
private double price;
//Stworzenie konstruktora.
public Samochód(String theCarBrand, String model, double capacity, String type, int engine, double yerOfProduction, String dateOfFirstRegistration, double price) {
this.theCarBrand = theCarBrand;
this.model = model;
this.capacity = capacity;
this.type = type;
this.engine = engine;
this.yerOfProduction = yerOfProduction;
this.dateOfFirstRegistration =dateOfFirstRegistration;
this.price = price;
}
// metody zwracające i ustawiające wartości.
public void setTheCarBrand(String t) {
theCarBrand = t;
}
public String getTheCarBrand() {
return theCarBrand;
}
public void setModel(String m) {
model = m;
}
public String getModel() {
return model;
}
public void setCapacity(double c) {
capacity = c;
}
public double getCapacity() {
return capacity;
}
public void setType( String ty) {
type = ty;
}
public String getType() {
return type;
}
public void setEngine(int e) {
engine = e;
}
public int getEngine() {
return engine;
}
public void setYerOfProduction(double y) {
yerOfProduction = y;
}
public double getYerOfProduction() {
return yerOfProduction;
}
public void setDateOfFirstRegistration(String d) {
dateOfFirstRegistration = d;
}
public String getDateOfFirstRegistration() {
return dateOfFirstRegistration;
}
public void setPrice(double p) {
price =p;
}
public double getPrice() {
return price;
}
// Metoda sprawdzająca czy Auto ma gwarancje;
public void czy_ma_gwarancje(String d)
{
LocalDate now = LocalDate.now();
LocalDate reg = LocalDate.parse(d, DateTimeFormatter.ISO_LOCAL_DATE);
Duration diff = Duration.between(reg.atStartOfDay(), now.atStartOfDay());
long minione_dni = diff.toDays();
if(minione_dni<=730)
{
System.out.println("Posiada gwarancję.");
}
else System.out.println("Nie posiada gwarancji.");
}
}
package zadania_lab5;
import java.util.ArrayList;
public class AutoKomis {
public static void main(String[] args) {
// tworzę tablicę obiektów klasy Samochód.
ArrayList<Samochód> listCars = new ArrayList<Samochód>();
// Dodaję obiekty do listy.
listCars.add(new Samochód (" Toyota " , " jaris " , 1000, "sedan", 65, 2000, " 2017.11.20 ", 3000 ));
listCars.add(new Samochód ("Mazda"," 3 ", 1560," kombi ", 105,2004," 2006.11.20 ", 4600));
listCars.add(new Samochód ("Volkswagen","Beetle", 2000, "Hatchback", 60, 2002, "2002.11.20",5499));
listCars.add(new Samochód ("Nissan", "Almera", 2000, "sedan", 75, 1999, "2001.11.12", 1900 ));
listCars.add(new Samochód ("Nissan", "Qashqai", 2500, "sedan", 170, 2008, "2017.11.20", 27900 ));
listCars.add(new Samochód ("Nissan", "Micra", 1240, "sedan", 65, 2004, "2005.11.20", 7000));
listCars.add(new Samochód ("Mazda", "6", 2000, "sedan",150 ,2009 , "20011.11.11",20000));
listCars.add(new Samochód ("BMW", " E39",1500 , " kombi",104 , 2005 , " 2005.11.11", 10000));
listCars.add(new Samochód ("Nissan", "primera", 2000 , " sedan", 150,2003 , "2004.11.11", 8000));
listCars.add(new Samochód ("volkswagen", " golf", 1500 , " sedan ", 108 , 1999 , "2000.12.12", 1000));
// Wyświetlenie tablicy obiektów i wywołanie metody sprawdzającej czy auto ma gwarancje.
for(Samochód x: listCars) {
System.out.println("Marka samochodu to: " + x.getTheCarBrand());
System.out.println("Model samochodu to: " + x.getModel());
System.out.println("Pojemność samochodu to:" + x.getCapacity() + "cm sześciennych");
System.out.println("Typ samochodu to: " + x.getType());
System.out.println("Moc silnika samochodu to: " + x.getEngine() + " Km");
System.out.println("Rok produkcji samochodu to: " + x.getYerOfProduction());
System.out.println("Data pierwszej rejestracji to:" + x.getDateOfFirstRegistration());
System.out.println("Cena samochodu to: " + x.getPrice() + " zł");
x.czy_ma_gwarancje("2017-11-20");
}
}
}
Problem z tym , że nie wiem o co chodzi w tym zadaniu.To znaczy wiem , że chodzi o te słowa kluczowe public , private itd. ale nie wiem jak się za to zabrać , czytałem trochę w internecie na ten temat ale też średnio to zrozumiałem.Czy ktoś mógłby mi najlepiej na jakimś przykładzie wytłumaczyć jak się zabrać za to zadanie?.Z góry dziękuję.
Pozdrawiam