CHAPTER 3
String Variables - enclosed in quotes
Declare:
String name;
*String is a class and name is an object
name=reader.readLine(); //reads a string value
Primitive Data Types
int
double
char
boolean
Modulus(%)
Remainder after division of two numbers
Type Casting
5/4=1 integer division
5.0/4=1.25 double division
ans=(double)num1/num2; //temporarily makes num1 a double in order to do double divistion
ans=(int)num1/num2; //temporarily makes num1 an integer to prevent an error and allow integer division
Character Variable Type (char) - single unicode symbol
char symbol;
symbol = 'b'; //opostrophes
symbol = reader.readChar();
Boolean - evaluates to either true or false (1 or 0)
boolean isWorking;
isWorking = True;
Programming Errors
Syntax Error - violation of syntax rule
- the compiler is unable to translate into byte code
- states error and line #
- check line above
- found during compile time
Run-Time Error - program preforms an illegal operatoin
- such as division by 0
- Null Pointer Exception - trying to use object's method before instantiating
- variable has not been initialized
- Exception in thread "main" - generally no semicolon at end of main line
Logic Error
- wrong output
- use test data to predict output and prevent wrong output
Debugging
- print out variables and check values through out program
/******************
Project 3.2
*******************/
import TerminalIO.KeyboardReader;
public class Project32
{
public static void main(String [] args)
{
KeyboardReader reader=new KeyboardReader();
double radius;
System.out.print("Enter the radius of a sphere: ");
radius=reader.readDouble();
System.out.println("Diameter: "+(2*radius));
System.out.println("Circumference: "+(2*3.14*radius));
System.out.println("Surface Area: "+(4*3.14*radius*radius));
System.out.println("Volume: "+((4/3.0)*3.14*radius*radius*radius));
}
}
/* OUTPUT
Enter the radius of a sphere: 2.6
Diameter: 5.2
Circumference: 16.328000000000003
Surface Area: 84.90560000000002
Volume: 73.58485333333334
Press any key to continue...
*/
Home