

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























1
UNIVERSITY of GREENWICH
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);
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
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?
8
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
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
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);
body of the loop which
is repeatedly executed
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
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
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 ( 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