Last active
October 12, 2018 00:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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