Math.sqrt()

public class Center
{
    public static void main(String[] args)
    {
        Picture pic = new Picture();
        pic.load("queen-mary.png");
        pic.draw();

        double centerX = pic.getWidth() / 2;
        double centerY = pic.getHeight() / 2;
        double radius = pic.getHeight() / 4;

        for (int x = 0; x < pic.getWidth(); x++)
        {
            for (int y = 0; y < pic.getHeight(); y++)
            {
                double distance = Math.sqrt((centerX - x)*(centerX - x) + (centerY - y)*(centerY - y));
                if (distance > radius)
                {
                    pic.setColorAt(x, y, Color.BLACK);
                }
            }
        }
    }
}
import java.util.Random;

public class test
{
    public static void main(String[] args)
    {
        Random generator = new Random(42);

        int numberOfSixes = 0;
        for(int i = 0; i < 101; i++)
        {
          int value = generator.nextInt(6) + 1;
          if(value == 6)
          {
            numberOfSixes++;
          }
        }
        System.out.println(numberOfSixes);
    }
}

Clock time

public class test
{
    public static void main(String[] args)
    {
        for (int minute = 0; minute < 60; minute++)
        {
            for (int hour = 1; hour < 12; hour++)
            {
                System.out.printf("%d:%02d ", hour, minute);
            }
            System.out.println();
        }
    }
}

algorithm

public class test
{
    public static void main(String[] args)
    {
        // Please do not modify this line.
        int numberOfRows = 5;

        for(int row = 1; row <= numberOfRows;row++)
        {
          for(int column = 1; column <= row;column++)
          {
            System.out.printf("[]");
          }
          System.out.println();
        }
    }
}

vowls

public class Vowels
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a word: ");
    String word = in.next().toLowerCase();

    int count = 0;

    for(int i = 0; i < word.length(); i++)
    {
      String letter = word.substring(i, i + 1);
      if ("aeiouAEIOU".contains(letter))
      {
        count++;
      }
    }
    System.out.println(count + " vowels");
  }
}
public class Negative
{
  public static void main(String[] args)
  {
    Picture pic = new Picture();
    pic.load("eliza.png");
    pic.draw();
    pic.pause();
    for(int i = 0; i < pic.pixels(); i++)
    {
      Color c = pic.getColorAt(i);

      Color negative = new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue());

      pic.setColorAt(i, negative);
    }
  }
}
public static void main(String[] args)
 throws FileNotFound Exception
 {
 	File inputFile = new File("input.txt");
 	Scanner in = new Scanner(inputFile);

 	while (in.hasNextDouble())
 	{
 		double value = in.nextDouble()
 	}
 }

public class Colors
{
public static void main(String[] args)
{
final int WIDTH = 30;
final int ROWS = 16;
final int COLUMNS = 16;

for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { int x = WIDTH * j; int y = WIDTH * i; Rectangle rect = new Rectangle(x, y, WIDTH - 1, WIDTH - 1); int blue = i * 255 / (ROWS - 1); int green = i * 255 / (COLUMNS - 1); Color fillColor = new Color(0, blue, green); rect.setColor(fillColor); rect.fill(); } } } } [/java]

compare dicimal

public class test
{
  public static void main(String[] args)
  {
    double original = 2;
    double root = Math.sqrt(original);
    double rootSquared = root * root;
    if (rootSquared == original)
    {
      System.out.println("They are the same");
    } else {
      System.out.println("rootSquared is " + rootSquared);
    }
  }
}
[vagrant@localhost new]$ java test
rootSquared is 2.0000000000000004
public class test
{
  public static void main(String[] args)
  {
    double original = 2;
    double root = Math.sqrt(original);
    double rootSquared = root * root;
    final double EPSILON = 1E - 12;
    if (Math.abs(rootSquared - original) < EPSILON)
    {
      System.out.println("They are the same");
    } else {
      System.out.println("rootSquared is " + rootSquared);
    }
  }
}

How long does it take to be millionare

public class test
{
  public static void main(String[] args)
  {
    double balance = 100;
    double target = 1000000;
    double rate = 0.01;
    int year = 0;
    while (balance < target)
    {
      double interest = balance * rate;
      balance = balance + interest;
      year++;
      System.out.printf("Year %d: %8.2f\n", year, balance);
    }
  }
}

sum is digit

int n = 365;
int sum = 0;
while (n > 0)
{
  int digit = n % 10;
  sum = sum + digit;
  n = n / 10;
}
System.out.println(sum);

while loop

while (balance >= target)
{
  double interest = balance * rate
  balance = balance + interest;
  year++;
  System.out.printf("Year %d: %8.2 year, balance")
}

The for loop
int counter = 1;
while (counter <= 6) { System.out.println(counter + ". Sleep"); couter++; } [/java]

cast

double price = 4.35
int pennies = (int)(4.35 * 100);

turn Gray

    public void turnGray()
    {
        int gray = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
    }
Math.pow(10, 3)
Math.sqrt(4)
Math.abs(3- 5)
Math.min(300, 255)
Math.max(0, -1)

math function

red = Math.min(255, red + 25);
import java.util.Scanner;

public class InputDemo
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		System.out.print("How old ar you? ");
		int age = in.nextInt();

		System.out.print("Next year, you will be ");
		System.out.println(age + 1);

		System.out.print("What is your weight? ");
		double.weight = in.nextDouble();

		System.out.print("I hope next year that'll be ");
		System.out.print(weight * 0.95);
	}
}
int cookiesPerDay;
double cerealBoxesPerDay;
String name;
System.out.printf("%6d", cookiesPerDay);
System.out.printf("%4.2f", cerealBoxesPerDay);
System.out.printf("%s", name);
String name = "whitehouse";

name.length()
name.substring(3, 7)
name.indexOf("c")
"Hello" + name

secret government elevator

import java.util.Scanner;

public class ElevatorDemo
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Floor: ");
        int floor = in.nextInt();
        int actualFloor;

        if (floor > 14)
        {
            actualFloor = floor - 2;
        }
        else
        {
            actualFloor = floor;
        }
        System.out.println("Actual floor: " + actualFloor);
    }
}

checker board

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;
         }
     }
}

閏年

西暦が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;
    }
}