1 / 6

1. Ett problem/uppgift

1. Ett problem/uppgift. Turtle =========== xPos: int yPos: int direction: double bodyColor: Color name: String .... ---------------------- forward(int): void backwark(int):void setName(String). World ============ width: int height: int background:Color ...

tamar
Download Presentation

1. Ett problem/uppgift

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1. Ett problem/uppgift

  2. Turtle =========== xPos: int yPos: int direction: double bodyColor: Color name: String .... ---------------------- forward(int): void backwark(int):void setName(String) ..... World ============ width: int height: int background:Color ... ------------------------ newColor(Color) ... 1. Ett problem/uppgift 2. Beskriv de typer av objekt som finns i klassdiagram. Klassdiagrammet har (liksom klassen) två huvuddelar: - instansvariabler med typ - metoder med parameterlista och returtyp

  3. Import java.util.*; public class Turtle { private int xPost; private int yPos private double direction; private Color bodycolor private String name; public Turtle(World w) { .... } public setName(String newName) { name = newName; } ..... } public class World { private int height; private int width; private Color background; public World(int w, int h) { .... } public setColor(Color newC) { background = newC; } ..... } 3. Implementera (koda) klasserna. Spara i filer, t ex Turtle.java

  4. public class Turtle { private int xPost; private int yPos private double direction; .... public Turtle(World w) { .... public setName(String n) { name = n; } ..... } public class World { private int height; private int width; private Color background; ... public World(int w, int h) { .... } public setColor(Color newC) { background = newC; } .... } . World w = new World(400,400); Turtle t1 = new Turtle(w); Turtle t2 = new Turtle(w); Turtle t3 = new Turtle(w); t1.turn(90); t1.forward(100); t2.forward(70); 4. MEN: vi måste skapa objekt och anropa metoder, annars händer inget {

  5. public class Turtle { private int xPost; private int yPos private double direction; .... public Turtle(World w) { .... } ..... } public class World { private int height; private int width; private Color background; ... public World(int w, int h) { .... } .... } public class TurtleTest { public static void main(String[] arg) { World w = new World(400,400); Turtle t1 = new Turtle(w); Turtle t2 = new Turtle(w); Turtle t3 = new Turtle(w); t1.turn(90); t1.forward(100); t2.forward(70); } } 5.Satserna skall ligga i en speciell metod som skall heta main. Lägg main-metoden i en egen klass, T ex TurtleTest. 6. Kompilera alla klasserna => - kompilatorn testar om syntaxen är korrekt - Sen översätts koden till java- Bytekod (class-filer)

  6. 7. Nu kan vi köra filen som innehåller main-metoden. > run TurtleTest

More Related