ICS3U Grade 11 Computer Science Java Basics Test

Chapter 4

Declaration: Declare variables taking the form <type> <name>;

-able to determine many variables at a time <type> <name1>, <name2>;

Data Type: Determines the type of data the variable will hold

Identifier: Name of the variable

Assignment: Variable name on the left and operation on right of equal sign

Concatenation: use + operation to connect the variables with the statement

Data Types:

-int: positive or negative without decimals

-double: positive or negative with decimals

-char: single character

-boolean: true or false

Floating point: meaning doesn’t have a whole number, contains decimal places

Abstract datatypes

-able to defined variables from a class made into an object

<class> <variable name> = new <class>(<arguments>)

-To access a member of a class like a method, it will be states as

System.out.println(“Radius of spot is “ + spot.getRadius());

Java Packages

-Import java packages to use them before.

-java.lang package allows you use util packages and different datatypes

-java.util needs to be imported to use Input stream scanner method.

Obtaining from user

-Input stream receives data from user input

-through the class Scanner

Next() : returns a string from output stream

nextLine() : returns a string up to the end of line character of input stream

nextInt() : returns an Int from the input stream

nextDouble() : returns a double from input stream

nextBoolean() : returns a Boolean read from the input stream

close() : closes the input stream

Numeric Expressions

-Integer Division: does normal division

-When both operands are integer, it will return an integer

-When both operands are double, it will return a double

-Modulus: Outputs the remainder.

Type Casting: Converts one number type to another

-Double > integer truncates (removes) the decimal portion of the double

-Rounding: Rounding simulated through Double > int by adding 0.5 to number before casting.

When casting a negative number, 0.5 should be subtracted from the casting

-When processing result, new data type name can be added before variable name

Int i, j;

Result = (double)i +(double)j;  changes the int to double

Formatting numbers

-import java.text file in order to use NumberFormat class to format numbers

NumberFormat <new object name> = NumberFormat.getCurrencyInstance();

-Can be currency ($), number, decimal, or percent