rectangle

Status
Nicht offen für weitere Antworten.
S

Stefan5678

Gast
Hallo Leute ich hab folgendes Problem:

Muss eine Klasse Rectangle (Rechteck) erstellen mit 3 verschiedenen
Konstruktoren und verschiedenen anderen get und set Methoden.
Methoden: (ich schreib die auf englisch auf)

A default constructor that initializes xPos and yPos to zero and width and height
to one.
A constructor that takes two double parameters which specify values for width
and height (in that order).
A constructor that takes four double parameters which specify values for xPos,
yPos, width and height (in that order).
void setPos(double x, double y)
double getXPos()
double getYPos()
double getWidth()
double getHeight()
double getArea() should return width*height
String toString() should return the string "Rectangle (xPos, yPos) - (xPos+width,
yPos+height)", with xPos, yPos, width and height replaced by the actual values
void printShape() should print the rectangle on the screen using * characters. The
double values used for position and size can be rounded into int values by casting.

In der Main sollen dann 2 Rechteckobjekte erstellt werden und einmal über
System.out.println(rectangle); und einmal über rectangle.printShape
ausgegeben werden. Wobei das "rectangle" durch den jeweiligen Objektnamen
ersetzt werden muss. Also z.b. Rechteck1 und Rechteck2 oder so.

Also letztes sollst du noch eine TestRectangles.java machen.

Dafür musst du die Rectangle.java "compareable" machen. Wie das genau geht,
weiß ich auf anhieb nicht mehr.

Und du sollst wohl 10 Rechtecke in eine geeignete Collection speichern und
diese dann mit dem TestRectangles vergleichen und quasi der Größe nach
sortieren. Also nach Flächeninhalt.

die ganze Aufgabe nochmal in englisch falls ich was flasch übersetzt habe ;-)

Rectangle.java
1. Create a class named Rectangle. It should have the following private fields which represent
the position and size of a rectangle in 2D:
· A double named xPos
· A double named yPos
· A double named width
· A double named height
It should have the following public methods:
· A default constructor that initializes xPos and yPos to zero and width and height
to one.
· A constructor that takes two double parameters which specify values for width
and height (in that order).
· A constructor that takes four double parameters which specify values for xPos,
yPos, width and height (in that order).
· void setPos(double x, double y)
· double getXPos()
· double getYPos()
· double getWidth()
· double getHeight()
· double getArea() should return width*height
· String toString() should return the string "Rectangle (xPos, yPos) - (xPos+width,
yPos+height)", with xPos, yPos, width and height replaced by the actual values
· void printShape() should print the rectangle on the screen using * characters. The
double values used for position and size can be rounded into int values by casting.
2. Add a main method (public static void main(String [] args)), that...
· Creates the following objects:
o A default Rectangle
o A Rectangle that goes from (2,2) to (4,6)
· Performs System.out.println(rectangle); for both of the above objects.
· Performs rectangle.printShape(); for both of the above objects.
TestRectangles.java
This assignment will get you using collections, comparators and the online documentation!

Bitte um eure Hilfe, weil ich jetzt zimmlich ratlos bin.
Vielen Dank im Vorraus.

Gruß
Stefan
 
B

Beni

Gast
Fang mal zuoberst an, und arbeite bis du feststeckst. Die ersten paar Sätze sind ja ziemlich eindeutig.

Hier wird dir niemand einfach so die Hausaufgaben machen; aber bei konkreten Problemen (was ein "ich bin ziemlich ratlos" nicht ist...) wird dir geholfen. Also zeig, was du schon gemacht hast!
 
S

Stefan5678

Gast
Code:
public class Rectangle
{
  private final String SIGN = "\u0065";

  private double xPos;
  private double yPos;
  private double width;
  private double height;

  public Rectangle()
  {
    this.xPos = this.yPos = 0;
  }

  public Rectangle(double w,double h)
  {
    this.width  = w;
    this.height = h;
  }

  public Rectangle(double x,double y,double w,double h)
  {
    this(w,h);
    this.xPos = x;
    this.yPos = y;
  }
  
  public void setPos(double x, double y)
  {
    this.xPos = x;
    this.yPos = y;
  }

  public double getXPos()
  {
    return this.xPos;
  }

  public double getYPos()
  {
    return this.yPos;
  }

  public double getWidth()
  {
    return this.width;
  }

  public double getHeight()
  {
    return this.height;
  }

  public double getArea()
  {
    return this.width * this.height;
  }

  public String toString()
  {
    return "Rectangle (" + this.xPos + ", " + this.yPos + ") - (" + (this.xPos + this.width) + ", " + (this.yPos+this.height) + ")";
  }

  public void printShape()
  {
    String output  = "";
    String spacesX = "";

    for(int g = 0; g < (int)this.yPos-1.0; g++)
    {
      output += "\n";
    }
    
    for(int h = 0; h < (int)this.xPos; h++)
    {
      spacesX += " ";
    }

    for(int i = 0; i < (int)this.height; i++)
    {

      if(i == 0 || i == (int)this.height-1.0)
      {
          output += spacesX;
        for(int j = 0; j < (int)this.width; j++)
        {
          output += this.SIGN;
        }
          output += "\n";
      }
      else
      {
          output += spacesX + this.SIGN;
        for(int k = 0; k < (int)(this.width-2); k++)
        {
          output += " ";
        }
          output += this.SIGN + "\n";
        
      }
    }
    
    System.out.println("====  RESULT: PRINTSHAPE()  ====");
    System.out.println();
    System.out.println(output);
  }

  public static void main(String[] argv)
  {
    // creating a default one
    Rectangle defaultRect = new Rectangle();
    System.out.println(defaultRect.toString());
    defaultRect.printShape();

    // a rect from (2,2) to (4,6)
    Rectangle secondOne = new Rectangle(2,2,2,4);
    System.out.println(secondOne.toString());
    secondOne.printShape();

    // a rect from (2,2) to (12,12)
    Rectangle thirdOne = new Rectangle(2,2,10,10);
    System.out.println(thirdOne.toString());
    thirdOne.printShape();
  }
}

Das habe ich bis jetzt gemacht. Und es ist mir schon klar das mir niemand alles programmiert. Da fehlt leider noch der letzte Teil von der Aufgabe und vielleicht können wir den zusammen machen?

Gruß
Stefan
 
S

Stefan5678

Gast
Also das müsste ich nich machen.

The Rectangle class should be modified to implement Comparable such that the
compareTo method orders rectangles by area.
· Your new program should be run by typing java TestRectangles - i.e. you will require
a class named TestRectangles that contains your main method.
· You need to select (and be able to justify) a suitable Collection class to store a
number of Rectangle objects.
· When the program is run, you should create ten rectangles using random, but
meaningful values for xPos, yPos, width and height and store them in the collection
you chose. Then you should output the collection of rectangles in the order
they were created. Afterwards you should sort the rectangles by area (see the documentation
for Collections.sort) and output the rectangles sorted by area.

Gruß
Stefan
 

Wildcard

Top Contributor
Ist eigentlich ganz einfach:
-Lass deine Klasse Comparable implementieren
Dazu musst du die Methode int compareTo(Object o) implementieren.
Als Object o werden dir andere Rectangles übergeben mit denen du das aktuelle dann vergleichen musst.
Wenn das andere Rectangle nach deinen Kriterien größer ist gibst du eine Zahl <0 zurück.
Ist es gleich groß 0, und ansonsten eine Zahl >0.
Anschließend musst du 10 Rectangles erzeugen und in eine ArrayList einfügen.
Zum sortieren einfach
Code:
Collections.sort(meineListeMitRectangles);
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben