Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Rectangletester;
- import java.util.Scanner;
- public class rectangleProgram {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- Rectangle rec1 = new Rectangle();
- Rectangle rec2 = new Rectangle();
- rec1.setLength(4.5);
- rec1.setHeight(5.5);
- System.out.println("The Area Surface is: " + rec1.surface(rec1.getLength(), rec1.getHeight()));
- System.out.println("The Scope is: " + rec1.scope(rec1.getLength(), rec1.getHeight()));
- rec1.starPrint(4.5, 5.5);
- System.out.println("Enter the character to print with");
- rec1.setCH(s.nextLine().charAt(0));
- rec1.charPrint(rec1.getLength(), rec1.getHeight(), rec1.getCH());
- //Check rec2
- rec2.setLength(-5);
- rec2.setHeight(-5);
- rec2.starPrint(rec2.getLength(), rec2.getHeight()); // it should print the default 10 BY 10
- //================================================================================================================
- package Rectangletester;
- public class Rectangle {
- private double _length, _height;
- private char _CH;
- /**
- * @param _length
- * @param _hieght
- * @param _CH
- * Constructor with the default of 10 BY 10 and * as the printing
- * character
- */
- public Rectangle() {
- super();
- this._length = 10;
- this._height = 10;
- this._CH = '*';
- }
- public Rectangle(double length, double hieght, char CH) {
- this._length = length;
- this._height = hieght;
- this._CH = CH;
- }
- // Duplicate constructor of the ORIGIONAL
- public Rectangle(Rectangle other) {
- _length = other._length;
- _height = other._height;
- _CH = other._CH;
- }
- // Builder without the Character
- public Rectangle(double length, double height) {
- this(length, height, ' ');
- }
- // _length getter
- public double getLength() {
- return _length;
- }
- // length Setter with checking if user enter a negative length or hieght
- public void setLength(double length) {
- if (_length <= 1 || _height <= 1) {
- System.out.println(" Length and Height of a RectAngle has to be greator than 0");
- System.out.println(" Default Length of RectAngle will Equal 10.0 X 10.0 cm. ");
- this._length = 10; // set length and height to 10 and printing with
- this._height = 10;
- this._CH = '*';
- } else {
- this._length = length;
- }
- }
- // height getter
- public double getHeight() {
- return _height;
- }
- // Height setter
- public void setHeight(double height) {
- if (_length <= 1 || _height <= 1) {
- System.out.println(" Length and Hieght of a RectAngle has to be greator than 0");
- System.out.println(" Default Length of RectAngle will Equal 10.0X10.0 cm. ");
- this._length = 10; // set length and height to 10 and printing with
- this._height = 10;
- this._CH = '*';
- } else {
- this._height = height;
- }
- }
- // CH Getter from the user
- public char getCH() {
- return _CH;
- }
- // CH Setter
- public char setCH(char CH) {
- return this._CH = CH;
- }
- // Printing Rectangle using the Math.floor method to round Length and Height
- public void starPrint(double length, double height) {
- for (int i = 1; i <= Math.floor(height); i += 1) {
- for (int j = 1; j <= Math.floor(length); j += 1)
- System.out.print("*");
- System.out.println();
- }
- System.out.println();
- }
- // Printing Rectangle using the Math.floor method to round Length and Height
- // to print w user Input
- public void charPrint(double length, double height, char CH) {
- for (int i = 1; i <= Math.floor(height); i += 1) {
- for (int j = 1; j <= Math.floor(length); j += 1)
- System.out.print(this._CH);
- System.out.println();
- }
- System.out.println();
- }
- // method calculate Surface or Area
- public double surface(double length, double height) {
- double surface = 0;
- surface = length * height;
- return surface;
- }
- // Method to calculate Scope the outer length and Height of Rectangle
- public double scope(double length, double height) {
- double scope = 0;
- return scope = 2 * length + 2 * height;
- }
- }
- //================================================================================
- package PersonTester;
- public class Person {
- private String _name;
- private float _height;
- private float _weight;
- public Person() {
- }
- public Person(String name, float height, float weight) {
- setName(name);
- setHeight(height);
- setWeight(weight);
- }
- private Person(Person person) {
- this(person.getName(), person.getHeight(), person.getWeight());
- }
- public String getName() {
- return _name;
- }
- public float getHeight() {
- return _height;
- }
- public float getWeight() {
- return _weight;
- }
- public void setName(String name) {
- _name = name;
- }
- public void setHeight(float height) {
- _height = height;
- }
- public void setWeight(float weight) {
- _weight = weight;
- }
- public String show() {
- return toString();
- }
- public String toString() {
- return getName() + ": Height: " + getHeight() + " and Weight: "
- + getWeight();
- }
- public Person clone() {
- return new Person(this);
- }
- public boolean equals(Object person) {
- if (!(person instanceof Person)) {
- return false;
- }
- Person p = (Person) person;
- if (p.getName().equals(getName()) && p.getHeight() == getHeight()
- && p.getWeight() == getWeight()) {
- return true;
- }
- return false;
- }
- public static void orderArrayByHeight(Person[] people) {
- // Short to Tall
- Person temp;
- for (int i = 1; i < people.length; i += 1) {
- if (people[i].getHeight() > people[i - 1].getHeight()) {
- temp = people[i];
- people[i] = people[i - 1];
- people[i - 1] = temp;
- i = 0;
- }
- }
- }
- public static void orderArrayByWeight(Person[] people) {
- // Light Weight to Heavy
- Person temp;
- for (int i = 1; i < people.length; i += 1) {
- if (people[i].getWeight() < people[i - 1].getWeight()) {
- temp = people[i];
- people[i] = people[i - 1];
- people[i - 1] = temp;
- i = 0;
- }
- }
- } // end of array
- } // End of Person Class
- //======================================================================================================
- package PersonTester;
- public class PersonProgram {
- public static void main(String[] args) {
- //Scanner s = new Scanner(System.in);
- Person[] people = new Person[10];
- fillRandomPersonArray(people);
- System.out.println(" People's Array generated Randomly");
- System.out.println("=================================================");
- for(Person p: people){
- System.out.println(p.show());
- }
- Person.orderArrayByHeight(people);
- System.out.println("\nAfter arranging by height (tallest to shortest)");
- System.out.println("================================================");
- for(Person p: people){
- System.out.println(p);
- }
- Person.orderArrayByWeight(people);
- System.out.println("\nAfter arranging by weight (lightest to heaviest)");
- System.out.println("=================================================");
- for(Person p: people){
- System.out.println(p);
- }
- }
- //name: fillRandomPersonArray
- //args: Person[] people - an array of type Person
- //description: fills an array of type Person with random entries. the name will be "name"+indexNumber
- //returns: N/A (void) but changes the given array
- public static void fillRandomPersonArray(Person[] people){
- for(int i=0; i<people.length; i+=1){
- people[i]= new Person("Person"+i,(int)(Math.random()*85)+115,(int)(Math.random()*50)+50);
- //there is implicit casting from int to float
- /*
- * Person p = new Person("name", height, weight);
- * for our purposes is the same as:
- * p.setName("name");
- * p.setHeight(height);
- * p.serWeight(weight);
- */
- }
- }
- }
- //=================================================================================================================
- /**
- *
- */
- /**
- * @author Rushdy
- *
- */
- public class Adult {
- private String name;
- private float height;
- private String job;
- /**
- *
- */
- public Adult() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @param name
- * @param height
- * @param job
- */
- public Adult(String name, float height, String job) {
- super();
- if (this.name == null) {
- this.name = " Person";
- }
- this.height = height;
- this.job = job;
- }
- public Adult(String name, float height) {
- this(name, height, null);
- }
- // Copy Constructor
- public Adult(Adult other) {
- name = other.name;
- height = other.height;
- job = other.job;
- }
- public Adult(String job) {
- this(" ", 0, job);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public float getHeight() {
- return height;
- }
- public void setHeight(float height) {
- this.height = height;
- }
- public String getJob() {
- return job;
- }
- public void setJob(String job) {
- this.job = job;
- }
- public void print(String name, float height, String job) {
- if (job == null)
- {
- System.out.println(" Person name: " + name + " Person Height: " + height);
- }
- else
- {
- System.out.println(" Person name: " + name + " Person Height: " + height + " Person Job :" + job);
- }
- }
- }
- //=========================================================================================================================
- public class adultProgram {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Adult person1 = new Adult();
- person1.setName("geege");
- person1.setHeight(190);
- person1.setJob("Teacher");
- person1.print(person1.getName(), person1.getHeight(), person1.getJob());
- Adult person2 = new Adult();
- person2.setName("feefe");
- person2.setHeight(180);
- person2.print(person2.getName(),person2.getHeight(),person2.getJob()
- );
- Adult person3 = new Adult();
- person3.setName("Meeme");
- person3.setHeight(190);
- person3.setJob("Teacher");
- person3.print(person3.getName(), person3.getHeight(), person3.getJob());
- person3.setJob("Programmer");
- person3.print(person3.getName(), person3.getHeight(), person3.getJob());
- }
- }
Add Comment
Please, Sign In to add comment