Potrzebuje pomocy przy tworzeniu losowych obiektów w javie

0

Witam, mam pilny problem bo musze do jutra wysłać zadanie a nie mam w ogóle pojęcia jak je zrobić. Bardzo chętnie przyjmę kazdą pomoc.

Treść zadania:
Zaprojektuj a następnie zaimplementuj hierarchię dziedziczenia dla własnych, wymyślonych przykładów, podobnie jak instrumenty i kształty (np. pojazdy, zwierzęta). Typ bazowy zawiera metody wspólne dla wszystkich klas z hierarchii. Typ potomny realizuje zachowania specyficzne dla klas potomnych. Tak jak w pliku Music.java stwórz obiekty losowo.

Kod Music.java

class Note 
{
  private int value;
  private Note(int val) 
  { 
	  value = val; 
  }
  public static final Note
    MIDDLE_C = new Note(0), 
    C_SHARP  = new Note(1),
    B_FLAT   = new Note(2);
} // Etc.

class Instrument 
{
  public void play(Note n) 
  {
    System.out.println("Instrument.play()");
  }
}

// Wind objects are instruments
// because they have the same interface:
class Wind extends Instrument 
{
  // Redefine interface method:
  public void play(Note n) 
  {
    System.out.println("Wind.play()");
  }
}

public class Music 
{
  public static void tune(Instrument i) 
  {
    // ...
    i.play(Note.MIDDLE_C);
  }
  public static void main(String[] args) 
  {
    Wind flute = new Wind();
    tune(flute); // Upcasting
  }
}

Kod Music3.java

import java.util.*;

class Instrument {
  public void play() {
    System.out.println("Instrument.play()");
  }
  public String what() {
    return "Instrument";
  }
  public void adjust() {}
}

class Wind extends Instrument {
  public void play() {
    System.out.println("Wind.play()");
  }
  public String what() { return "Wind"; }
  public void adjust() {}
}

class Percussion extends Instrument {
  public void play() {
    System.out.println("Percussion.play()");
  }
  public String what() { return "Percussion"; }
  public void adjust() {}
}

class Stringed extends Instrument {
  public void play() {
    System.out.println("Stringed.play()");
  }
  public String what() { return "Stringed"; }
  public void adjust() {}
}

class Brass extends Wind {
  public void play() {
    System.out.println("Brass.play()");
  }
  public void adjust() {
    System.out.println("Brass.adjust()");
  }
}

class Woodwind extends Wind {
  public void play() {
    System.out.println("Woodwind.play()");
  }
  public String what() { return "Woodwind"; }
}

public class Music3 {
  // Doesn't care about type, so new types
  // added to the system still work right:
  static void tune(Instrument i) {
    // ...
    i.play();
  }
  static void tuneAll(Instrument[] e) {
    for(int i = 0; i < e.length; i++)
      tune(e[i]);
  }
  public static void main(String[] args) {
    Instrument[] orchestra = new Instrument[5];
    int i = 0;
    // Upcasting during addition to the array:
    orchestra[i++] = new Wind();
    orchestra[i++] = new Percussion();
    orchestra[i++] = new Stringed();
    orchestra[i++] = new Brass();
    orchestra[i++] = new Woodwind();
    tuneAll(orchestra);
  }
}

No i teraz to co przerobiłem z poprzednich jakichś tam zadań

class Gryzon {
public void skok() 
{
	System.out.println("Gryzon skacze"); 
}
public void bieg() 
{ 
	System.out.println("Gryzon biegnie"); 
}
public void mnozenie() 
{ 
	System.out.println("Gryzon mnoży się"); 
}
public String toString() { return "Gryzon"; }
}
////////////////////////////////////////////////////
class Mysz extends Gryzon {
public void skok() 
{ 
	System.out.println("Mysz skacze"); 
}
public void bieg() 
{ 
	System.out.println("Mysz biegnie"); 
}
public void mnozenie() 
{ 
	System.out.println("Mysz mnoży się"); 
}
public String toString() { return "Mysz"; }
}
//////////////////////////////////////////////////////
class Wiewiórka extends Gryzon {
public void skok() 
{ 
	System.out.println("Wiewiórka skacze"); 
}
public void bieg() 
{ 
	System.out.println("Wiewiórka biegnie"); 
}
public void mnozenie() 
{ 
	System.out.println("Wiewiórka mnoży się"); 
}
public String toString() { return "Wiewiórka"; }
}
/////////////////////////////////////////////////////////
class Chomik extends Gryzon {
public void skok() 
{ 
	System.out.println("Chomik skacze"); 
}
public void bieg() 
{ 
	System.out.println("Chomik biegnie"); 
}
public void mnozenie() 
{ 
	System.out.println("Chomik mnoży się"); 
}
public String toString() { return "Chomik"; }
}
//////////////////////////////////////////////////////////////////
public class Zad12 {
public static void main(String args[]) {
Gryzon[] Gryzonie = {new Mysz(),new Wiewiórka(),new Chomik(),};
for(Gryzon r : Gryzonie) 
	{
	r.skok();
	r.bieg();
	r.mnozenie();
	System.out.println(r);
	}
}
}

Problem jest w tym że nie mam pojęcia na jakiej zasadzie mam stworzyć obiekty losowo bo ja nie do końca rozumiem ten kod...

0

Może jestem ślepy, ale nie widze tu żadnego "losowego tworzenia".

0

Coś takiego

Random r = new Random();
//losujemy rozmiar tablicy, rozmiar z przedziału [3,7]
Gryzon[] gryzonie = new Gryzon[3+r.nextInt(5)];
//losujemy rodzaj gryzonia
for(i=0;i<gryzonie.length;i++)
{
    int i = r.nextInt(3);
    switch(i):
    {
       case 0:
          gryzonie[i] = new Chomik();
          break;
       case 1:
          gryzonie[i] = new Mysz();
          break;
       case 2:
          gryzonie[i] = new Wiewiorka();
          break;
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1