public class CheckerBoardPainter { /** * Spaces are red if one of the coordinates is even and the other is odd * Spaces are blue if both coordinates are even * Spaces are blue if both coordinates are odd * * @return 1 for red spaces and 0 for blue * @param row the row of the space to paint * @param col the column of the space to paint. */ public int getColor(int row, int col) { if (row % 2 == 0 && col % 2 == 0) { return 0; } else if (row % 2 == 1 && col % 2 == 1) { return 0; } else { return 1; } } }
Month: January 2017
閏年
西暦が400で割り切れる年はうるう年である
400で割り切れない場合、西暦が100で割り切れる年はうるう年ではない
100で割り切れない場合、西暦が4で割り切れる年はうるう年である
4で割り切れない場合、うるう年ではない
much better to understand
ex if n % 400 != 0 && n % 100 = 0
return うるう年
n = 2000
-> 閏年
n = 1900
-> not 閏年
n = 2100
-> not 閏年
1900, 1, 1 Monday
1989, 7, 1
(364 + 365 * 88 + 22 + (31 + 28 + 31 + 30 + 31 + 30 + 1)) % 7 = 5
-> saturday
here is Java code
public class WhatDayWillItBe { public static void main(String[] args) { Day desiredDay = new Day(1983, 6, 18); Day referenceDay = new Day(1900, 1, 1); int referenceWeekDay = 1; int daysBetween = desiredDay.daysFrom(referenceDay): int uncorrectedWeekDay = referenceWeekDay + daysBetween; int weekday = uncorrectedWeekDay % 7; System.out.print("Weekday: "); System.out.println(weekday); } }
overflow of java
largest number of java integer is about two billion. if variable integer over it, it get overflow.
public class MathInJava { public double amdahlSpeedup(double s, double n) { return 1 / (s + (1 - s)/n); } public double crossRatio(double a, double b, double c, double d) { return ((a-c)*(b-d))/((b-c)*(a-d)); } public double average(double a, double b, double c, double d) { // To do! return (a + b + c + d)/ 4.0; } }
public void reduce() { red = (red * 6 / 256) * 51; green = (green * 6 / 256) * 51; blue = (blue * 6 / 256) * 51; }
connect friend class
public class PersonDemo { public static void main(String[] args) { Person sara = new Person("Sara", "sara.jpg", 10, 200); Person chenghan = new Person("Cheng-Han", "cheng-han.png", 300, 0); Person cay = new Person("Cay", "cay.gif", 250, 180); } } // Bluej project: lesson3/friends4 public class Person { private String name; private String friends; private int x; private int y; public Person (String aName, String pictureName, int xCoord, int yCoord) { name = aName; friends = ""; x = xCoord; y = yCoord; Picture picture = new Picture(pictureName); picture.translate(xCoord, yCoord); picture.draw(); } public void addFriend(Person friend) { friends = friends + friend.name + " "; SmallCircle circle = new SmallCircle(x, y); circle.fill(); Line line = new Line(x, y, friend.x, friend.y); line.draw(); } public void unfriend(Person nonFriend) { friends = friends.replace(nonFriend.name + " ", ""); } public String getFriends() { return friends; } }
The this Reference
public double drive(double distance) { this.mimlesDriven = this.milesDriven + distance }
public Person (String name, String pictureName, int x, int y) { this.name = name; this.friends = ""; this.x = x; this.y = y; Picture picture = new Picture(pictureName); picture.translate(x, y); picture.draw(); }
draw picture class
public class PersonDemo { public static void main(String[] args) { Person sara = new Person("Sara", "sara.jpg", 10, 200); Person chenghan = new Person("Cheng-Han", "cheng-han.png", 300, 0); Person cay = new Person("Cay", "cay.gif", 250, 180); } }
public class Person { private String name; private String friends; public Person (String aName, String pictureName, int xCoord, int yCoord) { name = aName; friends = ""; Picture picture = new Picture(pictureName); picture.translate(xCoord, yCoord); picture.draw(); } public void addFriend(Person friend) { friends = friends + friend.name + " "; } public void unfriend(Person nonFriend) { friends = friends.replace(nonFriend.name + " ", ""); } public String getFriends() { return friends; } }
GasInTank
instance variable stay long, but local variable only stay short term.
public class CarTester { public static void main(String[] args) { Car car = new Car(); // TODO: Add 20 gallons and drive 100 miles car.addGas(20); car.drive(100); // TODO: Print actual and expected gas level System.out.println("Expected:18"); System.out.println(car.getGasInTank()); } }
public class Car { private double milesDriven; private double gasInTank; private double milesPerGallon; public Car() { milesDriven = 0; gasInTank = 0; milesPerGallon = 50; } /** Drives this car by a given distance. @param distance the distance to drive */ public void drive(double distance) { milesDriven = milesDriven + distance; double gasConsumed = distance / milesPerGallon; gasInTank = gasInTank - gasConsumed; } public void addGas(double amount) { gasInTank = gasInTank + amount; } /** Gets the current mileage of this car. @return the total number of miles driven */ public double getMilesDriven() { return milesDriven; } /** Gets the current amount of gas in the tank of this car. @return the current gas level */ public double getGasInTank() { return gasInTank; } }
test programming
public class test { public static void main(String[] args) { Rectangle box = new Rectangle(45, 90, 60, 90); box.grow(20, 20); system.out.println(box.getX()); system.out.println("exected: 25"); system.out.println(box.getY()); system.out.println("exected: 70"); } }
Rectangle frontFace = new Rectangle(20, 30 ,100, 40); Line leftLine = new Line(20, 30, 50, 10); Line topLine = new Line(50, 10, 150, 10); Line middleLine = new Line(120, 30, 150, 10); Line rightLine = new Line(150, 10, 150, 50); Line bottomLine = new Line(120, 70, 150, 50); frontFace.draw(); leftLine.draw(); topLine.draw(); middleLine.draw(); rightLine.draw(); bottomLine.draw();
box.setColor(new Color(255, 0, 0)); box.draw(); box2.setColor(new Color(0, 255, 0)); box2.fill()
Object oriented programming
java put name before the variable name.
public class test { public static void main(String[] args) { Picture rocket = new Picture("mariner4.jpg"); rocket.translate(200, 200); rocket.draw(); Picture planet = new Picture("mars.gif"); planet.grow(-50, -50); planet.draw(); } }
public class test { public static void main(String[] args) { Day birthday = new Day(1951, 5, 26); Day lastDay = new Day(2012, 5, 26); int daysAlive = lastDay.daysFrom(birthday); System.out.println(daysAlive); } }
variable can vary and assignment has no type.
int numberOfPixels = object.methodname(arguments)
public class test { public static void main(String[] args) { Rectangle box = new Rectangle(60, 90, 20, 30); Rectangle box2 = new Rectangle(80, 120, 20, 30); box.draw(); box2.draw(); } }
public class test { public static void main(String[] args) { String river = "Mississippi"; int numberOfLetters = river.length(); System.out.println(numberOfLetters); } }
replace method
public class test { public static void main(String[] args) { String river = "Mississippi"; String result = river.replaceAll("i","x"); System.out.println(result); } }
cut space with trim()
String messyString = "Hello, Space!"; messyString.trim()
AccessorとMutator:「Accessor=アクセスするヤツ」、「Mutator=mutate(訳すと変化させる)」
An accessor method is used to return the value of a private field.
A mutator method is used to set a value of a private field. It follows a naming scheme prefixing the word “set” to the start of the method name.
Implement test program, set actual result and expected result.
Test program
public class test { public static void main(String[] args) { System.out.println("Hello\nWorld!"); } }
print and println
public class test { public static void main(String[] args) { System.out.print(3); System.out.println(4 + 5); } }
compile error
public class test { public static void main(String[] args) { System.ouch.println(4 + 5); } }
Hadoop
The reason to use big data is it’s too big to store in one machine.Challenges with big data is data is created fast and data from different source in various formats.
Hadoop
Store in HDFS
process with MAPREDUCE
Hadoop ecosystem
pig, hive … select * from
mapreduce, impala, hbase
HDFS <- sqoop, flume
Hue, oozie, mahout
Cloudera is a distribution of Hadoop(CDH)
Hadoop picks three node as random.