Core Java

Fundamental Programming Structures in Java

cover

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

Java 9 News Flash - JShell

java9news

Cursor Keys and Tab Completion

JShell Tips

Java 10 News Flash - Local Type Inference

java9news

Work with Strings and the API documentation

Strings

String Concatenation

More About Strings

Code Points and Code Units

The String API

Java 9 News Flash - API Search

java9news

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

03_09

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

03_10

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

The do/while Statement

03_11

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

03_12

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

LotteryOdds

The switch Statement

03_13

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

Java 9 News Flash - BigInteger.TWO

java9news

Use arrays to store multiple elements of the same type

Arrays

Supplying Element Values

Copying Arrays

LotteryDrawing

Multidimensional Arrays

CompoundInterest

LotteryArray