Java Programming: Variables, Maths, Control Structures, and Classes, Study notes of Mobile Computing

Java code examples and explanations on various topics including variables, math operations, control structures (if, if-else, for loop), and classes. It covers the basics of java programming and is suitable for university students and lifelong learners.

Typology: Study notes

2010/2011

Uploaded on 09/08/2011

rossi46
rossi46 🇬🇧

4.5

(10)

313 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 2
Java Quick Start – Part 2
Stelios Kapetanakis & Markus A. Wolf
Based on material from Gill Windall
1
Application Development
for Mobile Devices
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Java Programming: Variables, Maths, Control Structures, and Classes and more Study notes Mobile Computing in PDF only on Docsity!

Lecture 2

Java Quick Start – Part 2

Stelios Kapetanakis & Markus A. Wolf

Based on material from Gill Windall

1

Application Development

for Mobile Devices

Outline

Examples

1. HelloWorld (previous lecture)

2. Variables and Maths

3. Ifs and Strings

4. Arrays and loops

5. Classes

Sources of further information

Exercises

UNIVERSITY of GREENWICH

Marks1.java

  • Marks1.java

package jqs;

public class Marks1 {

public static void main(String[] args) {

int mark1 = 56,

mark2 = 72;

int total = mark1 + mark2;

System.out.println("The total is " + total);

int average = total / 2;

System.out.println("The average is " + average);

Variables of the

built-in types

  • Marks1.java

package jqs;

public class Marks1 {

public static void main(String[] args) {

int mark1 = 56 ,

mark2 = 72 ;

int total = mark1 + mark2;

System.out.println("The total is " + total);

int average = total / 2;

System.out.println("The average is " + average);

int is one of the 8 built-in types in Java, the full set are:

byte, short, int, long

char

float, double

boolean

Maths question

public class MathsQuestion {

public static void main(String[] args) {

int a = 10, b = 20 , c = 3;

a += b;

a++;

c *= a;

c += a;

System.out.println("a = " + a + " b = " + b + " c = " + c);

What will be

printed?

Example 3

Ifs and Strings

8

Strings

public class Marks2 {

public static void main(String[] args) {

int mark1 = 56,

mark2 = 72;

String student1 = "Mohammed",

student2 = "Delia";

Unlike int, String is not built-in data type but a class in the Java

API. (So why don't we need to import it?)

student1 and student2 are more like the variable called "now" of

type Date that we saw in the first example. They are references to

instances of class String.

:String

Mohammed

:String

Delia

Ifs

if (mark1 == mark2) {

System.out.println("Both marks are the same");

else {

if (mark1 > mark2) {

System.out.println(student1 + "'s mark is higher");

else {

System.out.println(student2 + "'s mark is higher");

Just like ifs in most programming languages, if in Java allows the

program to behave in different ways when it runs depending on

conditions

Example 4

Arrays and loops

13

MarksWithArray.java

public class MarksWithArray {

public static void main(String[] args) {

int [] marks = {56, 72, 19, 65};

for (int i = 0; i < marks.length; i++) {

System.out.println(marks[i]);

int total = 0;

for (int i = 0; i < marks.length; i++) {

total += marks[i];

System.out.println("Total is " + total);

System.out.println("Average is " + total / marks.length);

anatomy of a for loop

General format of a for loop

for ( initial statement ; test ; final statement ) {

statement 1;

statement 2;

statement n;

body of the loop which

is repeatedly executed

for (int i = 0; i < marks.length; i++) {

System.out.println(marks[i]);

executed once only

at the beginning

tested each time to

see if to carry on

with the loop

executed each time

round the loop after

executing the

statements

executing the for loop

[0] [1] [2] [3]

int [] marks = {56, 72, 19, 65};

for (int i = 0; i < marks.length; i++) {

System.out.println(marks[i]);

truefalse

41230

Output

56

72

19

65

for loop question

int [] numbs = new int[3];

for (int x = 0; x < numbs.length; x++) {

numbs[x] = x * 2;

for (int x = 0; x < numbs.length; x++) {

System.out.println(numbs[x]);

What will be

printed?

while and do/while loops

while ( test ) {

statement 1;

statement 2;

statement n;

do {

statement 1;

statement 2;

statement n;

} while ( test );

the body of the loop is executed zero

or more times

the body of the loop is executed

1 or more times