Skip to content

Instantly share code, notes, and snippets.

@technoglot
Last active May 1, 2019 02:54
Show Gist options
  • Save technoglot/fb2a95f6e1b0886fafa055288d12a968 to your computer and use it in GitHub Desktop.
Save technoglot/fb2a95f6e1b0886fafa055288d12a968 to your computer and use it in GitHub Desktop.
Collection of Java programming exercises introducing OOP concepts.
package fan;
public class Fan {
    public static void main(String[] args) {
    Fan1 fan = new Fan1();//object 1
    Fan1 fan1 = new Fan1();//object 2
    
    fan.setSpeed(fan.FAST); fan.setRadius(10); fan.setColor("yellow"); fan.setOn(true);
    fan1.setSpeed(fan1.MEDIUM); fan1.getRadius(); fan1.getColor(); fan.isOn();
    
      //Display the objects
    System.out.println("The first fan: " + fan.toString());
    System.out.println("The second fan: " + fan1.toString());
    }   
}

class Fan1 {
final int SLOW = 1, MEDIUM = 2, FAST = 3; 
private int speed = 1; 
private boolean on = false; 
private double radius = 5;
String color = "blue"; 

//Accessor & mutator methods
public int getSpeed(){
  return speed;    
}

public void setSpeed(int newSpeed){
  speed = newSpeed;  
}

public boolean isOn(){
  return on;    
}

public void setOn(boolean newOn){
  on = newOn;    
}

public double getRadius(){
  return radius;    
}

public void setRadius(double newRadius){
  radius = newRadius;    
}

public String getColor(){
  return color;    
}

public void setColor(String newColor){
  color = newColor;   
}

//no-arg constructor
Fan1(){
    
}

//Method
@Override
public String toString(){
    if(on == true) {
      return ("\nSpeed: " + speed + "\nColor: " + color + "\nRadius: " + radius + "\nFan is on\n" );}
    else {
      return ("\nColor: " + color + "\nRadius: " + radius + "\nFan is off\n" ); 
    }
  } 
}
package geometricobject;
import java.util.Scanner;

public class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 
 System.out.print("Enter side1 of the triangle: ");
 double s1 = input.nextDouble();
 System.out.print("Enter side2 of the triangle: ");
 double s2 = input.nextDouble();
 System.out.print("Enter side3 of the triangle: ");
 double s3 = input.nextDouble();
 System.out.print("Enter the color of the triangle: ");
 String color = input.next();
 System.out.print("Enter if the triangle is filled (use true or false): ");
 boolean fill = input.nextBoolean();
 
 Triangle triangle = new Triangle(s1, s2, s3);//create a triangle
 triangle.setSide1(s1);//set the sides of the triangle
 triangle.setSide2(s2);
 triangle.setSide3(s3);
 
 triangle.setColor(color);//set color and set filled
 triangle.setFilled(fill);
 
 System.out.println("The area of the triangle is: " + triangle.getArea());
 System.out.println("The perimeter of the triangle is: " + triangle.getPerimeter());
 System.out.println(triangle.toString());//returns string representation of the object
}

//Construct a default geometric object 
GeometricObject(){
  dateCreated = new java.util.Date();
}

//Construct a geometric object with the specified color and filled value 
GeometricObject(String color, boolean filled) {
  this.color = color;
  this.filled = filled;
}

//Return color 
public String getColor() {
  return color;
}

//Set a new color 
public void setColor(String color) {
  this.color = color;
}

//Return filled. Since filled is boolean, its get method is named isFilled 
public boolean isFilled() {
  return filled;
}

//Set a new filled 
public void setFilled(boolean filled) {
  this.filled = filled;
}

//Get dateCreated 
public java.util.Date getDateCreated() {
  return dateCreated;
}

//Return a string representation of this object 
@Override 
public String toString() {
  return "created on " + dateCreated + "\ncolor: " + color +
  " and filled: " + filled;
}
}  

//TRIANGLE CLASS
package geometricobject;

public class Triangle extends GeometricObject {
  double side1, side2, side3 = 1.0; boolean filled = false;
  String color = "white";
    
  //no-arg constructor
  Triangle(){
  }
  
  //second constructor
  Triangle(double side1, double side2, double side3){   
  }
   
  //getter/accessor methods for each data field
  public double getSide1(){
   return side1;    
  }
   
  public double getSide2(){
   return side2;    
  }
   
  public double getSide3(){
   return side3;    
  }
   
  //setter/mutator methods
  public void setSide1(double side1){
   this.side1 = side1;
  }
   
  public void setSide2(double side2){
   this.side2 = side2;
  }
   
  public void setSide3(double side3){
   this.side3 = side3;
  }
   
   //regular methods
  public double getArea(){
   return (0.5 * side1 * side2);    
  }
   
  public double getPerimeter(){
   return side1 + side2 + side3;    
  }
   
  @Override
  public void setColor(String color) {
    this.color = color;
  }
  
  @Override
  public boolean isFilled() {
    return filled;
  }
  
  @Override
  public void setFilled(boolean filled) {
    this.filled = filled;
  }

//overridden method
   @Override
   public String toString(){
     return "Triangle: side1 = " + side1 + " side2 = " + side2 +
     " side3 = " + side3 + " Color: " + color + " Filled: " + filled;   
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment