Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LAB 4
- ex1
- public class Circle {
- private double radius;
- private String color;
- public Circle() {
- this.radius = 1.0;
- this.color = "red";
- }
- Circle(double r) {
- radius = r;
- color = "pink";
- }
- Circle(double r, String c) {
- radius = r;
- color = c;
- }
- public double getRadius() {
- return radius;
- }
- public double getArea() {
- return 2 * Math.PI * radius;
- }
- }
- public class TestCircle {
- public static void main(String[] args) {
- Circle c1 = new Circle();
- System.out.println("The first circle has the length of the radius: " + c1.getRadius());
- Circle c2 = new Circle();
- System.out.println("The second circle has the area: " + c2.getArea());
- }
- }
- ex3
- public class Author {
- private String name;
- private String email;
- private char gender;
- public Author(String name, String email, char gender) {
- this.name = name;
- this.email = email;
- this.gender = gender;
- }
- public String getName() {
- return name;
- }
- public String getEmail() {
- return email;
- }
- public char getGender() {
- return gender;
- }
- public void setEmail(String email)
- {
- this.email = email;
- }
- public String toString() {
- String v = this.name + "(" + this.gender + ") at " + this.email;
- return v;
- }
- }
- public class TestAuthor {
- public static void main(String[] args) {
- Author a1 = new Author("Ana Popescu", "anapopescu@yahoo.com", 'f');
- Author a2 = new Author("Alexandru Rus", "alexandru.rus@yahoo.com",'m');
- Author a3 = new Author("Maria Campean", "mariacampean12@yahoo.com", 'f');
- System.out.println("The name of the first author is "+ a1.getName());
- System.out.println("The gender of the second author is "+ a2.getGender());
- System.out.println("The email of the third author is "+ a3.getEmail());
- String s = a1.toString();
- System.out.println("First author: "+s);
- a2.setEmail("alexandru_rus@gmail.com");
- System.out.println("The new email of the second author is "+a2.getEmail());
- }
- }
- ex3
- public class Book {
- private String name;
- private Author author;
- private double price;
- private int qtyInStock;
- public Book(String name, Author author, double price) {
- this.name = name;
- this.author = author;
- this.price = price;
- }
- public Book(String name, Author author, double price, int qtyInStock) {
- this.name = name;
- this.author = author;
- this.price = price;
- this.qtyInStock = qtyInStock;
- }
- public String getName() {
- return name;
- }
- public Author getAuthor() {
- return author;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public int getQtyInStock() {
- return qtyInStock;
- }
- public void setQtyInStock(int qtyInStock) {
- this.qtyInStock = qtyInStock;
- }
- @Override
- public String toString() {
- String v = this.getName() + " by " + this.author.toString();
- return v;
- }
- }
- public class TestBook {
- public static void main(String[] args) {
- Author a1 = new Author("Anna Todd", "annatodd@yahoo.com", 'f');
- Author a2 = new Author("Suzzane Collins", "suzzanecollins@yahoo.com", 'f');
- Author a3 = new Author("Veronica Roth", "veronicaroth@yahoo.com", 'f');
- Book b1 = new Book("After", a1, 25, 150);
- Book b2 = new Book("Hunger Games", a2, 15, 110);
- Book b3 = new Book("Divergent", a3, 20, 200);
- System.out.println("The author of the book After is " + b1.getAuthor());
- System.out.println("The second book is " + b2.getName());
- System.out.println("The price of the third book is " + b3.getPrice() + " dollars");
- System.out.println("There are " + b1.getQtyInStock() + " After books in stock");
- b2.setPrice(13);
- System.out.println("The new price of the Hunger Games book is " + b2.getPrice() + " dollars");
- b3.setQtyInStock(90);
- System.out.println("There are only " + b3.getQtyInStock() + " Divergent books in stock! Don't miss your chance to buy one!");
- System.out.println("The information about the third book is " + b3.toString());
- }
- }
- ex4
- public class Book {
- private String name;
- private Author[] authors;
- private double price;
- private int qtyInStock = 0;
- public Book(String name, Author[] authors, double price) {
- this.name = name;
- this.authors = authors;
- this.price = price;
- }
- public Book(String name, Author[] authors, double price, int qtyInStock) {
- this.name = name;
- this.authors = authors;
- this.price = price;
- this.qtyInStock = qtyInStock;
- }
- public String getName() {
- return name;
- }
- public Author[] getAuthors() {
- return authors;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price)
- {
- this.price = price;
- }
- public int getQtyInStock() {
- return qtyInStock;
- }
- public void setQtyInStock(int qtyInStock) {
- this.qtyInStock = qtyInStock;
- }
- @Override
- public String toString() {
- String v = this.getName() + " by " + authors.length + " authors ";
- return v;
- }
- public void printAuthors() {
- int i;
- for (i = 0; i<=authors.length; i++){
- System.out.println(this.authors[i].toString());
- }
- }
- }
- public class TestBook {
- public static void main(String[] args) {
- Author a1 = new Author("John Green", "johngreen@gmail.com", 'm');
- Author a2 = new Author("Maureen Johnson", "maureenjohnson@gmail.com", 'f');
- Author a3 = new Author("Lauren Myracle", "laurenmyracle@gmail.com", 'f');
- Author a4 = new Author("Stephanie Dray", "stephaniedray@yahoo.com", 'f');
- Author a5 = new Author("Vicky Alvear", "vickyalvear@yahoo.com", 'm');
- Author[] authors = new Author[3];
- authors[0] = a1;
- authors[1] = a2;
- authors[2] = a3;
- Book b1 = new Book("Let it snow", authors, 30);
- authors = new Author[2];
- authors[0] = a4;
- authors[1] = a5;
- Book b2 = new Book("A day of fire", authors, 26, 100);
- System.out.println("The second book is " + b2.getName());
- System.out.println("Authors: ");
- b2.printAuthors();
- System.out.println("The price of the first book is " + b1.getPrice() + " dollars");
- System.out.println("There are " + b1.getQtyInStock() + " Let it snow books in stock");
- b2.setPrice(13);
- System.out.println("The new price of the second book is " + b2.getPrice() + " dollars");
- b2.setQtyInStock(90);
- System.out.println("There are only " + b2.getQtyInStock() + " A day of fire books in stock!");
- System.out.println("The information about the first book:", b1.toString());
- }
- }
- ex5
- public class Cylinder extends Circle {
- private double height;
- private double volume;
- public Cylinder() {
- this.height = 1.0;
- }
- public Cylinder(double radius) {
- super(radius);
- }
- public Cylinder(double radius, double height) {
- super(radius);
- this.height = height;
- }
- public double getHeight() {
- return this.height;
- }
- @Override
- public double getArea() {
- double a=this.getRadius() * this.getRadius() * 3.14 * this.height;
- return a;
- }
- }
- public class TestCylinder {
- public static void main(String args[]) {
- Cylinder c1 = new Cylinder(2, 8);
- Cylinder c2 = new Cylinder(1, 5);
- Cylinder c3 = new Cylinder(4, 2);
- System.out.println("Area of c1 = " + c1.getArea());
- System.out.println("Area of c2 = " + c2.getArea());
- System.out.println("Area of c3 = " + c3.getArea());
- }
- }
- }
- ex 6
- public class Circle extends Shape {
- private double radius;
- public Circle() {
- this.radius = 1.0;
- }
- public Circle(double radius){
- this.radius = radius;
- }
- public Circle(double radius, String color, boolean filled){
- super(color,filled);
- this.radius = radius;
- }
- public double getRadius() {
- return radius;
- }
- public void setRadius(double radius) {
- this.radius = radius;
- }
- public double getArea(){
- return this.radius*this.radius*3.14;
- }
- public double getPerimeter(){
- return 2*3.14*this.radius;
- }
- @Override
- public String toString(){
- return "A Circle with radius "+this.radius+" which is a subclass of "+ super.toString();
- }
- }
- public class Rectangle extends Shape {
- private double width;
- private double length;
- public Rectangle() {
- this.width = 1.0;
- this.length = 1.0;
- }
- public Rectangle(double width, double length) {
- this.width = width;
- this.length = length;
- }
- public Rectangle(double width, double lenght, String color, boolean filled) {
- super(color, filled);
- this.width = width;
- this.length = lenght;
- }
- public double getWidth() {
- return width;
- }
- public void setWidth(double width) {
- this.width = width;
- }
- public double getLength() {
- return length;
- }
- public void setLength(double length) {
- this.length = length;
- }
- public double getArea() {
- return this.width * this.length;
- }
- public double getPerimeter() {
- return 2 * (this.width + this.length);
- }
- @Override
- public String toString() {
- return "Rectangle with width= " + this.width + " and length= " + this.length + " which is a subclass of " + super.toString();
- }
- }
- public class Shape {
- private String color;
- private boolean filled;
- public Shape() {
- this.color = "red";
- this.filled = true;
- }
- public Shape(String color, boolean filled) {
- this.color = color;
- this.filled = filled;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public boolean isFilled() {
- return this.filled;
- }
- public void setFilled(boolean filled) {
- this.filled = filled;
- }
- @Override
- public String toString() {
- String s;
- if (filled = true)
- s = "filled";
- else s = "not filled";
- return "Shape{" + "color:" + color + ", is " + s + "}";
- }
- }
- public class Square extends Rectangle {
- public Square() {
- }
- public Square(double side) {
- super(side, side);
- side = side;
- }
- public Square(double side, String color, boolean filled) {
- super(side, side, color, filled);
- side = side;
- }
- public double getSide() {
- return super.getLength();
- }
- public void setSide(double side) {
- this.setLenght(side);
- this.setWidth(side);
- }
- public void setWidth(double side) {
- super.setWidth(side);
- }
- public void setLenght(double side) {
- super.setLength(side);
- }
- public String toString() {
- return "A square with side = " + this.getSide() + "which is a subclass of " + super.toString();
- }
- }
- public class TestShape {
- public static void main(String[] args) {
- Shape s = new Shape("green",true);
- System.out.println(s.toString());
- Circle c = new Circle(3,"yellow",true);
- System.out.println(c.toString());
- System.out.println("Area = "+c.getArea());
- System.out.println("Perimeter = "+c.getPerimeter());
- System.out.println("Is filled = "+c.isFilled());
- Rectangle r = new Rectangle(4,6,"pink",false);
- System.out.println(r.toString());
- System.out.println("Width = "+r.getWidth());
- r.setWidth(5);
- System.out.println("New width = "+r.getWidth());
- System.out.println("Area = "+r.getArea());
- System.out.println("Is filled = "+r.isFilled());
- Square sq = new Square(10,"turquoise",false);
- sq.setSide(3);
- System.out.println(sq.toString());
- System.out.println("Is filled = "+sq.isFilled());
- System.out.println("Perimeter = "+sq.getPerimeter());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement