Array declaration

String[] words = new String[10];
String[] words = { "yes", "no" };
values[2] = values[1];
values.set(2, values.get(1));

Use hasNextDouble

public HomeworkScores(int maxScores)
    {
        System.out.println("enter socre 0 to quit: ");

        while(userInput.hasNextDouble())
        {
          double nextScore = userInput.nextDouble();
          socres[currentSize] = nextScore;
          currentSize++;
        }
    }
public double sumScores()
    {
        double sum = 0;
        for (double score: scores)
        {
            sum = sum + score; 
        }
        return sum;
    }

output average score

public double averageScore()
    {
        if (currentSize == 0)
        {
          return 0;
        }
        else {
          sumScores() / currentSize;
        }
    }

remove lowest score

public void removeLowest()
{
      double low = LowScore();
      int lowScoreIndex = find(low);
      remove(lowScoreIndex);
}

public void getLowScoreIndex()
{
double lowestScore = scores[0];
int lowestScoreIndex = 0;
for (int i = 1; i < currentSize; i++) { if (scores[i] < lowestScore) { lowestScore = scores[i]; lowestScoreIndex = i; } } return lowestScoreIndex; } [/java]

ArrayList

ArrayList<Color> palette = new ArrayList<Color>();

ArrayElement is simple than array list.

double[] values = new double[10];
double[] moreValues = { 32, 54, 67.5, 29, 35}
double firstValue = values[0];
values[0] = 42;

list

for (int i = 0; i < values.length; i++)
{
	System.out.println(values[i]);
}
for (double value: values)
{
	System.out.println(value);
}

array declare

int[] primes = { 2, 3, 5, 7, 11 };
int[] primes = { 2, 3, 5, 7, 11 };
for (int i = 0; i < 2; i++)
{
	primes[4 - i] = primes[i];
}
values[0] = 10;
values[values.length - 1] = 10;

find portait

import java.util.ArrayList;

public class ListOfPictures
{
    public static void main(String[] args)
    {
        ArrayList<Picture> gallery = new ArrayList<Picture>();
        gallery.add(new Picture("degas1.jpg"));
        gallery.add(new Picture("gaugin1.jpg"));
        gallery.add(new Picture("monet1.jpg"));
        gallery.add(new Picture("monet2.jpg"));
        gallery.add(new Picture("renoir1.jpg"));
        
        // Your code here
        Int count = 0;
        for(Picture pic: gallery)
        {
            if ( pic.getHeight() > pic.getWidth())
            {
                counter++;
            }
        }
        System.out.println("Pictures with portrait orientation: " + count);
    }
}

find match
i = 0
found = false
while not found and i < size if ith element matches found = true else i++ if found, then i is index of of match

import java.util.ArrayList;

public class ListOfPictures
{
    public static void main(String[] args)
    {
        ArrayList gallery = new ArrayList();
        gallery.add(new Picture(“degas1.jpg”));
        gallery.add(new Picture(“gaugin1.jpg”));
        gallery.add(new Picture(“monet1.jpg”));
        gallery.add(new Picture(“monet2.jpg”));
        gallery.add(new Picture(“renoir1.jpg”));
        
        int i = 0;
        boolean found = false;
        
        while (!found && i < gallery.size())
        {
            Picture pic = gallery.get(i)
            if(pic.getHeight() > pic.getWidth())
            {
                found = true;
            }
            else
            {
              i++;
            }
        }
        
        if (found)
        {
            gallery.get(i).draw();
        }
    }
}

arrya list

import java.util.ArrayList;

public class ListOfPictures
{
    public static void main(String[] args)
    {
        ArrayList<Picture> gallery = new ArrayList<Picture>();
        gallery.add(new Picture("degas1.jpg"));
        gallery.add(new Picture("gaugin1.jpg"));
        gallery.add(new Picture("monet1.jpg"));
        gallery.add(new Picture("monet2.jpg"));
        gallery.add(new Picture("renoir1.jpg"));

        ArrayList<Picture> matches = new ArrayList<Picture>();
        for(Picture pic: gallery)
        {
            if(pic.getHeight() > pic.getWidth())
            {
                matches.add(pic);
            }
        }

        int rightmostX = 0;
        for (Picture pic : matches)
        {
            pic.translate(rightmostX + 10, 0);
            rightmostX = pic.getMaxX();
            pic.draw();
        }
    }
}
    public String getFriends(String separator)
    {
        String separatedFriends = "";
        for (int i = 0; i < friends.size(); i++)
        {
          if (i > 0)
          {
            separatedFriends = separatedFriends + separator + friend.get(i);
          }
          else 
          {
            separatedFriends = separatedFriends + friend.get(i);
          }
        }
    }
public void unfriend(Person nonFriend)
{
   int nonFriendIndex = find(nonFriend);
   if (nonFriendIndex != -1)
   {
     friends.remove(nonFriendIndex);
   }   
}

use find

public void talkTo(Person person)
{
    int oldIndex = find(person);
    if(oldIndex != -1)
    {
      friends.remove(oldIndex);
      friends.add(0, person);
    }
}

get pic width

 import java.util.ArrayList;

public class ListOfPictures
{
    public static void main(String[] args)
    {
        ArrayList<Picture> gallery = new ArrayList<Picture>();
        gallery.add(new Picture("degas1.jpg"));
        gallery.add(new Picture("gaugin1.jpg"));
        gallery.add(new Picture("monet1.jpg"));
        gallery.add(new Picture("monet2.jpg"));
        gallery.add(new Picture("renoir1.jpg"));
        
        // Your code here
        int sum = 0;
        for (Picture pic: gallery)
        {
          sum = sum + pic.getWidth();
        }         
        System.out.println("Sum of widths: " + sum);
    }
}

sort
largest = inital element
for all other elements
if measure(element) > measuer(largest)
largest = element

import java.util.ArrayList;

public class ListOfPictures
{
    public static void main(String[] args)
    {
        ArrayList<Picture> gallery = new ArrayList<Picture>();
        gallery.add(new Picture("degas1.jpg"));
        gallery.add(new Picture("guigou1.jpg"));
        gallery.add(new Picture("gaugin1.jpg"));
        gallery.add(new Picture("monet1.jpg"));
        gallery.add(new Picture("seurat1.jpg"));
        
        // Your code here  
        Picture tallest = gallery.get(0);
        for (Picture element: gallery)
        {
            if (element.getHeight() > tallest.getHeight())
            {
                tallest = element;
            }
        }
        System.out.println("Tallest height: " 
           + tallest.getHeight());
        tallest.draw();
    }
}

translate array list

import java.util.ArrayList;

public class ListOfPictures
{
    public static void main(String[] args)
    {
        ArrayList gallery = new ArrayList();
        gallery.add(new Picture("degas1.jpg"));
        gallery.add(new Picture("gaugin1.jpg"));
        gallery.add(new Picture("monet1.jpg"));
        gallery.add(new Picture("monet2.jpg"));
        gallery.add(new Picture("renoir1.jpg"));

        for (int i = 0; i < gallery.size(); i++)
        {
            Picture pic = gallery.get(i);
            pic.translate(100 * i, 0);
            pic.draw();
        }
    }
}

fixed it

import java.util.ArrayList;

public class ListOfPictures
{
    public static void main(String[] args)
    {
        ArrayList gallery = new ArrayList();
        gallery.add(new Picture("degas1.jpg"));
        gallery.add(new Picture("gaugin1.jpg"));
        gallery.add(new Picture("monet1.jpg"));
        gallery.add(new Picture("monet2.jpg"));
        gallery.add(new Picture("renoir1.jpg"));

        for(int i = 1; i < gallery.size(); i++)
        {
            Picture pic = gallery.get(i);
            Picture left = gallery.get(i - 1);
            pic.translate(left.MaxX() + 10, 0);
        }
        
        for (int i = 0; i < gallery.size(); i++)
        {
            Picture pic = gallery.get(i);
            pic.draw();
        }
    }
}
Picture old = gallery.get(2);
gallery.set(2, gallery.get(1));
gallery.set(1, old);

Picture pic = gallery.get(1);
gallery.set(2, pic)
gallery.add(0, pic)
gallery.remove(2);

Picture last = gallery.remove(gallery.size() - 1);
gallery.add(0, last);
import java.util.ArrayList;
public class test
{
    public static void main(String[] args)
    {
        ArrayList<String> booksToRead = new ArrayList<String>();

        booksToRead.add("The Eyre Affair by Jasper Fforde");
        booksToRead.add("Night Watch by Terry Pratchett");
        booksToRead.add("Next by Michael Crichton");
        booksToRead.add("The Brain That Changes Itself by Norman Doidge");
        booksToRead.add("Effective Java by Joshua Bloch");
        booksToRead.add("The Visual Display of Quantitative Information by Edward R. Tufte");

        String anotherBook = "Why zebras don't get ulcers by Robert M. Sapolsky";
        booksToRead.add(2, anotherBook);

        System.out.println(booksToRead);
    }
}

using "set" for replace

import java.util.ArrayList;
public class test
{
    public static void main(String[] args)
    {
        ArrayList<String> booksToRead = new ArrayList<String>();

        booksToRead.add("The Eyre Affair by Jasper Fforde");
        booksToRead.add("Night Watch by Terry Pratchett");
        booksToRead.add("Next by Michael Crichton");
        booksToRead.add("The Brain That Changes Itself by Norman Doidge");
        booksToRead.add("Effective Java by Joshua Bloch");
        booksToRead.add("The Visual Display of Quantitative Information by Edward R. Tufte");

        String anotherBook = "Why zebras don't get ulcers by Robert M. Sapolsky";
        booksToRead.add(2, anotherBook);

        String sequel = "Lost in a Good Book by Jasper Fforde";
        booksToRead.set(0, sequel);

        // Please don't modify the following line:
        System.out.println(booksToRead);
    }
}

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