Java Syntax: COP3330 Fall 2006 University of Central Florida, Study Guides, Projects, Research of Computer Science

An outline for the java syntax course cop3330 offered at the university of central florida in fall 2006. Topics covered include classes and methods, primitives and variables, strings, input and output, modifiers, control structures, constructors, and more. Students will learn the basics of java programming through lectures and exercises.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 11/08/2009

koofers-user-h72
koofers-user-h72 🇺🇸

10 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Java Syntax
COP3330 – Fall 2006
University of Central Florida
H. Schwartz
Outline
Classes and Methods
Primitives and Variables
Strings
Input and Output
Modifiers
Control Structures
Constructors
COP 3330 – Fall 2006
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Java Syntax: COP3330 Fall 2006 University of Central Florida and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Java Syntax

COP3330 – Fall 2006

University of Central Florida

H. Schwartz

Outline

• Classes and Methods

• Primitives and Variables

• Strings

• Input and Output

• Modifiers

• Control Structures

• Constructors

COP 3330 – Fall 2006

Classes and Methods

• Comments:

• Represent: Always include name, NID, COP3330,

project title, and date.

Java Syntax - COP 3330 – Fall 2006

// place two slashes before line comments

/* this is block comment style

everything between the slash-star and star-slash

is considered a comment and ignored by the

compiler.

Classes and Methods

• a class

Java Syntax - COP 3330 – Fall 2006

class FirstClassExample {

//class definition here

//notice the capitalization

public class FirstClassExample {

//class definition here

//notice the capitalization

Tangent:

Java and long

names

Classes and Methods

• Constructing an object out of a

class:

Java Syntax - COP 3330 – Fall 2006

public static void main (String [] args) {

//main method for class

//Construct a FirstClassExample object named “example”:

FirstClassExample example = new FirstClassExample();

Primitives and Variables

• Variables hold anything that can change (vary) during

execution.

= ;

• Objects can be assigned to variables.

Java Syntax - COP 3330 – Fall 2006

Primitives and Variables

• Java has a finite set of primitives:

• Primitives are not treated as objects

Java Syntax - COP 3330 – Fall 2006

NA boolean true, false false 1 bit used in 32 bit integer

Unicode character \u0000 16 bits \u0000 to \uFFFF char

+/-4.9E-324 to

+/-1.7976931348623157E+308,

+/-infinity, +/-0, NaN

double IEEE 754 floating point 0.0 64 bits

+/-1.4E-45 to +/-3.4028235E+38,

+/-infinity, +/-0, NaN

float IEEE 754 floating point 0.0 32 bits

-9223372036854775808 to

9223372036854775807

long signed integers 0 64 bits

int signed integers 0 32 bits -2147483648 to 2147483647

short signed integers 0 16 bits -32768 to 32767

-128 to 127 byte signed integers 0 8 bits

Type Values Default Size Range

Primitives and Variables

• Arithmetic Operators

– Standard syntax, Examples:

• Increment and Decrement

Java Syntax - COP 3330 – Fall 2006

x = y + z;

z = x * y;

int x = y – z;

z = x * (y + u) / a;

z = 2 * (10 + 12) / 4; //z = 2 * 22 = 44 / 4 = 11

x++; //same as x = x + 1, original x returned as value

x--; //same as x = x - 1, original x returned as value

++x; //same as x = x + 1, original x + 1 returned as value

x += 5; //same as x = x + 5

x -= 3; //same as x = x - 3

Input and Output

• Output

– System.out class member

– (System is class, out is object)

  • Common Methods:
    • print
    • println

Java Syntax - COP 3330 – Fall 2006

String sentence = “I love OOP”;

System.out.print(sentence);//no new line at the end

System.out.println(sentence);//new line at the end

System.out.println(“Who would actually say:\n” + sentence);

//use \n to put newline in middle, append sentence to end

Input and Output

• Input

– Scanner class:

– Scanner objects help extract values from the standard input.

Java Syntax - COP 3330 – Fall 2006

import java.util.;//Scanner is part of java.util.

Scanner stdin = new Scanner(System.in);

Input and Output

• Input

– Scanner methods

– A token is (generally) something separated by whitespace.

Java Syntax - COP 3330 – Fall 2006

Scanner stdin = new Scanner(System.in);

double aReal = stdin.nextDouble();//get a double from the input stream

int anInt = stdin.nextInt();//gets an int

String aString = stdin.next();//gets the next input as a String

String line = stdin.nextLine();//gets a whole line as a String

Modifiers

(parameter list) {

Statement list

}

• Choices:

– public/private/protected

– static

– final

– abstract

Java Syntax - COP 3330 – Fall 2006

Classes and Objects Revisited

• Instance Variables

Java Syntax - COP 3330 – Fall 2006

public class ClassWithInstanceVariables{

int number;

String name;

public ClassWithInstanceVariables(){

//constructor: sets initial values

this.number = 0;

this.name = “unknown”;

public void incrementPrintNum(){

//increments number and prints it’s original and new values

int oldNum = number;

this.number++;

System.out.print(oldNum + “, “ + this.number);

Methods Revisited

• Return values

– Standard syntax

Java Syntax - COP 3330 – Fall 2006

public int getNum() {

//returns an integer after doing something

int x = 5;

return x;

Methods Revisited

• Parameters..

Java Syntax - COP 3330 – Fall 2006

public int setNum(int n){

//sets instance variable number to n

this.number = n;

Public int setNumName(int n, String name){

//sets number and name

this.number = n;

this.name = name; //notice same variables name (this is important)

Operators revisited

• Integer Division ‘/’

– Everything after decimal truncated

• Modulo ‘%’

  • Remainder of division

Java Syntax - COP 3330 – Fall 2006

Operators revisited

• Overflow and Underflow

  • Value of result can not be stored in desired primitive type
  • Inaccurate result produced

Java Syntax - COP 3330 – Fall 2006

int biggestInt = Integer.MAX_VALUE;

System.out.println(biggestInt+1);

Control Structures

• Boolean operators

  • return true or false
  • (p && q) (p || q)
  • !p
  • (p == q) (p != q)
  • (p > q) (p >= q) (p < q) (p <= q)

Java Syntax - COP 3330 – Fall 2006

Control Structures

• if statement

• if ()

Java Syntax - COP 3330 – Fall 2006

int a = 10;

int b = 12;

if (a < b)

a++;

if (a >= b) {

a++;

b = a – 1;

Control Structures

• if else statement

if ()

else

Java Syntax - COP 3330 – Fall 2006

int a = 10;

int b = 12;

if (a < b)

a++;

else {

a++;

b = a – 1;

Control Structures

• for (; ; )

Java Syntax - COP 3330 – Fall 2006

for (int i = 0; i < 50; i++){

//prints 0 through 49

System.out.println(“I is ” + i);

Control Structures

• Nested Control Structures

  • Placing control structures within other structures

Java Syntax - COP 3330 – Fall 2006

int sum = 0;

for (int i = 0; i < 50; i++){

//takes the sum of factorials

int mult = 1;

for (int j = i; j > 0; j--){

//finds i factorial

mult = mult * j;

sum = sum + mult;

String location = house.getLoc();

if (location.equals(“orlando”)){

//decision to buy a house

int price = house.getPrice;

if (price < 100000)

house.buy();

else if (price < 120000)

house.bargain();

else

house.moveOn();

Constructors Revisited

• Multiple Constructors

  • Initialize different variables, or take in different parameters.

Java Syntax - COP 3330 – Fall 2006

String name;

int gpa;

public Student(String name, int gpa){

//constructor for all parameters

this.name = name;

this.gpa = gpa;

public Student(String name){

//constructor for name only

this.name = name;

this.gpa = 0;//don’t know gpa

More on objects

• Object Variable is a reference to an

object

– default value: null

– assignment operator copies location

– objects as parameters: passes location

Java Syntax - COP 3330 – Fall 2006

(student instance)

name = “Susan Smith“

numStudents

Student: susSmith