Core Java

Fundamental Programming Structures in Java

Copyright © Cay S. Horstmann 2016

Write a simple Java program

We will not use ‘Hello, World!’

Method Calls

Comments

FirstSample

Work with numeric data types

Integers

Floating-Point Numbers

The char Type

The boolean Type

Variables

Mathematical Operators and Functions

Type Conversions

More Operators

Enumerated Types

Work with Strings and the API documentation

Strings

String Concatenation

More About Strings

Code Points and Code Units

The String API

Write programs that read input and produce output

Console Input

Formatted Output

File Input and Output

InputTest

Use the control flow constructs of the Java language

The if Statement

if (yourSales >= 2 * target)
{
   performance = "Excellent";
   bonus = 1000;
}
else if (yourSales >= 1.5 * target)
{
   performance = "Fine";
   bonus = 500;
}
else if (yourSales >= target)
{
   performance = "Satisfactory";
   bonus = 100;
}
else
{
   System.out.println("You're fired");
}

The while Statement

while (balance < goal)
{
   balance += payment;
   double interest = balance * interestRate / 100;
   balance += interest;
   years++;
}
System.out.println(years + " years.");

The do/while Statement

do
{
   balance += payment;
   double interest = balance * interestRate / 100;
   balance += interest;
   year++;
   // print current balance
   . . .
   // ask if ready to retire and get input
   . . .
}
while (input.equals("N"));

Retirement

Retirement2

The for Statement

for (int i = 1; i <= 10; i++)
   System.out.println(i);

LotteryOdds

The switch Statement

Scanner in = new Scanner(System.in);
System.out.print("Select an option (1, 2, 3, 4) ");
int choice = in.nextInt();
switch (choice)
{
   case 1:
      . . .
      break;
   case 2:
      . . .
      break;
   case 3:
      . . .
      break;
   case 4:
      . . .
      break;
   default:
      // bad input
      . . .
      break;
}

The break Statement

The continue Statement

Work with big numbers when arbitrary precision is required

Big Numbers

BigIntegerTest

Use arrays to store multiple elements of the same type

Arrays

Supplying Element Values

Copying Arrays

LotteryDrawing

Multidimensional Arrays

CompoundInterest

LotteryArray