Skip to content

Instantly share code, notes, and snippets.

@w3cj
Last active June 21, 2019 15:06
Show Gist options
  • Save w3cj/4c85d49777cdf606e7b1605da99fa1a7 to your computer and use it in GitHub Desktop.
Save w3cj/4c85d49777cdf606e7b1605da99fa1a7 to your computer and use it in GitHub Desktop.
Live coded java examples
public class Car {
public int miles = 0;
public int miles() {
return 12;
}
public static void main(String[] args){
Car car = new Car();
// miles variable
System.out.println(car.miles); // prints 0
// miles() method
System.out.println(car.miles()); // prints 12
}
}
public class JStoJava {
public static void main(String[] args) {
String[] names = { "Alice", "Bob", "Cory" };
String result = "";
for (int i = 0; i < names.length; i++) {
result += names[i];
}
System.out.println(result);
}
}
public class Person {
public String first;
public String last;
public Person(String first, String last) {
this.first = first;
this.last = last;
}
public String fullName() {
return first + " " + last;
}
}
public class Program {
public static void main(String[] args) {
sayHello();
System.out.println(add(2, 2));
Person cj = new Person("CJ", "R");
System.out.println(cj.fullName());
Rectangle myRectangle = new Rectangle(100,100);
Rectangle yourRectangle = new Rectangle(500,50);
Rectangle.incrementTotal(10);
Rectangle.incrementTotal(10);
myRectangle.incrementTotal(10);
yourRectangle.incrementTotal(10);
System.out.println(myRectangle.area());
System.out.println(yourRectangle.area());
Rectangle.incrementTotal(10);
Rectangle.incrementTotal(10);
Rectangle.printTotal();
}
// function add(x, y) {
// return x + y;
// var name = 'CJ';
// var name = "CJ";
// };
public static int add(int x, int y) {
int sum = x + y;
return sum;
}
public static void sayHello() {
char a = 'a';
System.out.println("Hello World!");
}
// public static String add(int x, int y) {
// return "x plus y is " + (x + y);
// }
}
function Rect(width, length){
this.width = width;
this.length = length;
}
Rect.prototype.area = function(){
return this.width * this.length;
}
var myRect = new Rect(100,100);
console.log(myRect.area());
public class Rectangle {
public int width;
public int length;
public static int theTotal;
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public int area() {
return width * length;
}
public static void incrementTotal(int increment) {
theTotal += increment;
}
public static void printTotal() {
System.out.println(theTotal);
}
public static int sumAreas(Rectangle[] rectangles) {
}
}
public class Strings {
public static void main(String[] args) {
// creates an array that will only take Strings, and will only take 4 items
String[] names = new String[4];
// even though we haven't put anything in the array, it has a length of 4
System.out.println(names.length); // 4
// put string "Hello" in the 0th index;
names[0] = "Hello";
// put string "Wrold" in the 1st index;
names[1] = "World";
// length is still 4, even though we put 2 items in it.
System.out.println(names.length); // 4
// prints the memory address
System.out.println(names); // [Ljava.lang.String;@7852e922
doTheThing();
}
public static void doTheThing() {
String[] names = { "Su", "Will", };
System.out.println(names[0]);
String[][] fullNames = {
{"First", "Su"},
{"Last", "Sylvester"},
};
System.out.println(fullNames[0][0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment