Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active October 12, 2018 00:43
Show Gist options
  • Save harrisonmalone/ac5238c5040bc263ce1ee27dc2ec0143 to your computer and use it in GitHub Desktop.
Save harrisonmalone/ac5238c5040bc263ce1ee27dc2ec0143 to your computer and use it in GitHub Desktop.
java example with getters and setters, also uses the date and scanner utils, scanner util is like gets in ruby
import java.util.Date;
public class Car {
private String brand;
private String colour;
private int wheels;
private Date date = new Date();
public Car(String brand, String colour) {
this.brand = brand;
this.colour = colour;
this.wheels = 4;
this.date = date;
}
public String getBrand() {
return brand;
}
public int getWheels() {
return wheels;
}
public Date getDate() {
return date;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
}
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Car holden = new Car("holden", "red");
System.out.println(holden.getBrand() + " | " + holden.getWheels());
Scanner sc = new Scanner(System.in);
System.out.println("How many wheels does your car have?");
System.out.print("> ");
int i = sc.nextInt();
holden.setWheels(i);
System.out.println(holden.getBrand() + " | " + holden.getWheels() + " | " + holden.getDate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment