Dziesiąta prezentacja do kursu „Wstęp do programowania”.
Treść:
- Klasy i obiekty (druga część) – hermetyzacja, mutatory (setters), akcesory (getters), modyfikatory dostępu
- Dodawanie pakietów
- Listy tablicowe (ArrayList)
Ta strona jest częścią starego kursu Javy. Nowy kurs, pt. “Programowanie w Javie z elementami bioinformatyki dla poczatkujących”, jest znacznie rozszerzony i dostosowany fo Javy 8. Jego forma jest także znacznie bardziej przystępna dla korzystających wyłącznie z treści on-line. Dostępne materiały znajdziesz tutaj.
Trzy poniższe klasy powinny znaleźć się w jednym pakiecie „gatunki”.
Klasa główna Gatunki
package gatunki;
public class Gatunki {
	public static void main(String[] args) {
		Gatunek gatunek1 = new Gatunek();
		Gatunek gatunek2 = new Gatunek("Poa", "nemoralis");
		Gatunek gatunek3 = new Gatunek("Javacus", "programmus", 6, 3, "Uwag brak");
		gatunek1.drukujWszystkieDane();
		gatunek2.drukujWszystkieDane();
		gatunek3.drukujWszystkieDane();
		System.out.println(gatunek3.getPelnaNazwa()+
				" posiada haploidalną liczbe chromosomów = "+
				gatunek3.getHaploidalnaLiczbaChromosomow());
		Chromosom[] kariotyp = {new Chromosom(1, 3.0, 4.0),
								new Chromosom(2, 4.0, 4.2),
								new Chromosom(3, 5.0, 7.5)};
		gatunek3.kariotyp = kariotyp;
		gatunek3.drukujKariotyp();
	}
}
Klasa Gatunek
package gatunki;
public class Gatunek {
	String rodzaj;
	String gatunek;
	int n2;
	int x;
	String uwagi;
	Chromosom[] kariotyp;
	public Gatunek() {
		this.rodzaj = "nieznany";
		this.gatunek = "nieznany";
		this.n2 = 0;
		this.x = 0;
		this.uwagi = null;
	}
	public Gatunek(String rodzaj, String gatunek) {
		this.rodzaj = rodzaj;
		this.gatunek = gatunek;
		this.n2 = 0;
		this.x = 0;
		this.uwagi = null;
	}
	public Gatunek(String rodzaj, String gatunek, int n2, int x, String uwagi) {
		this.rodzaj = rodzaj;
		this.gatunek = gatunek;
		this.n2 = n2;
		this.x = x;
		this.uwagi = uwagi;
	}
	public String getPelnaNazwa() {
		return rodzaj+" "+gatunek;
	}
	public int getHaploidalnaLiczbaChromosomow() {
		return n2/2;
	}
	public void drukujWszystkieDane() {
		System.out.println("---------"+getPelnaNazwa()+"------------");
		System.out.println("Rodzaj:  "+rodzaj);
		System.out.println("Gatunek: "+gatunek);
		System.out.println("2n:      "+n2);
		System.out.println("x:       "+x);
		System.out.println("Uwagi:   "+uwagi);
		System.out.println("          ................... ");
	}
	public void drukujKariotyp() {
		for (int i=0; i<kariotyp.length; i++)="" {="" chromosom="" system.out.println("chromosom="" "+chromosom.getnumer()+="" "="" ramie="" 1:="" "+chromosom.getramie1()+="" 2:="" "+chromosom.getramie1());="" }="" [="" sourcecode]="" <h3="">Klasa <code>Chromosom</code>
[sourcecode language='java']
package gatunki;
public class Chromosom {
	private int numer;
	private double ramie1;
	private double ramie2;
	private boolean plciowy;
	private String typ;
	private String uwagi;
	public int getNumer() {
		return numer;
	}
	public void setNumer(int numer) {
		if (numer&amp;gt;=1) this.numer = numer;
		else System.out.println("Numer chromosomu nie może być mniejszy od 1!");
	}
	public double getRamie1() {
		return ramie1;
	}
	public void setRamie1(double ramie1) {
		if (ramie1&amp;gt;=1) this.ramie1 = ramie1;
		else System.out.println("Długość ramienia nie może być mniejsza od 1!");
	}
	public double getRamie2() {
		return ramie2;
	}
	public void setRamie2(double ramie2) {
		if (ramie2&amp;gt;=1) this.ramie1 = ramie2;
		else System.out.println("Długość ramienia nie może być mniejsza od 2!");
	}
	public boolean isPlciowy() {
		return plciowy;
	}
	public void setPlciowy(boolean plciowy) {
		this.plciowy = plciowy;
	}
	public String getTyp() {
		return typ;
	}
	public void setTyp(String typ) {
		this.typ = typ;
	}
	public String getUwagi() {
		return uwagi;
	}
	public void setUwagi(String uwagi) {
		this.uwagi = uwagi;
	}
	public Chromosom() {
		this.numer =0;
		this.ramie1 = 0.0;
		this.ramie2 = 0.0;
		this.plciowy = false;
		this.typ = null;
		this.uwagi = null;
	}
	public Chromosom(int numer, double ramie1, double ramie2) {
		this.numer = numer;
		this.ramie1 = ramie1;
		this.ramie2 = ramie2;
		this.plciowy = false;
		this.typ = null;
		this.uwagi = null;
	}
	public Chromosom(int numer, double ramie1, double ramie2, boolean plciowy, String typ) {
		this.numer = numer;
		this.ramie1 = ramie1;
		this.ramie2 = ramie2;
		this.plciowy = plciowy;
		this.typ = typ;
		this.uwagi = null;
	}
	public double getCalaDlugosc() {
		return ramie1+ramie2;
	}
}
Klasa Import
Importowanie pakietu, niektóre metody ArrayList.
import java.util.*;
public class Import {
	public static void main(String[] args) {
		ArrayList<string> lista = new ArrayList<string>();
		System.out.println(lista);
		lista.add("a");
		System.out.println(lista);
		lista.add("b");
		System.out.println(lista);
		lista.add(1, "c");
		System.out.println(lista);
		lista.get(2);
		System.out.println(lista);
		lista.remove(2);
		System.out.println(lista);
		lista.set(0, "u");
		System.out.println(lista);
		System.out.println(lista.size());
		System.out.println(lista);
		lista.clear();
		System.out.println(lista);
	}
}
 @grzegg
  @grzegg

 Loading...
Loading...
Leave a Reply
You must be logged in to post a comment.