18
Seminar: Coding Principles Topic Number: 2 Application Engineering and Development

Seminar 2 coding_principles

Embed Size (px)

Citation preview

Seminar: Coding Principles

Topic Number: 2

Application Engineering and Development

Key topics and learning outcomes of this Seminar

• Reinforce the Java code introduced during Lecture 2 by running Java code in IntelliJ;

• Develop confidence working with Intellij and logic/layout files;

• Develop Mobile Application Programming skills;

3

Open IntelliJ, Create New Project

B4004A L1 4

Select ‘Java’

B4004A L1 5

Select as Screenshot

B4004A L1 6

Add a Project name

B4004A L1 7

Opens Class File: Main.java

B4004A L1 8

Run the program, and check the terminal returns ‘Hello World!’

This is the ‘run’ button:

B4004A L1 9

Type some code into Main.java and run it:

Type this code into the same file in IntelliJ and run it. Some code will need to be deleted to reflect correctly the code below (ie the Hello World code section). Check the Terminal output. public class Main { public static void main(String[] args) { int a = 2;int b = 4; int c; c=a+b; System.out.print(c); } }

Change the variables a and b to different values and then run the code again and check the correct result shows in the Terminal.

B4004A L1 10

Create a for loop

Create a for loop: public class Main { public static void main(String[] args) { for(int i=0;i<10;i++) { int a = 2; int b = 4; int c; c = a + b; System.out.print(c); } } } … then make it print out each number on a new line by adding “\n” for a new line: System.out.print(c + "\n");

… change i<10 to i<20 and run the program again to see what happens

B4004A L1 11

Change code to make it add 1 to each number.

… then make the program add 1 to each number and print it out in the Terminal by adding:

a = a+1;

You will need to move the declaration (int a = 2; int b = 4; int c;) out of the for loop, otherwise the result will remain static as the program will always go back to those values. Try running it without moving the declaration first, check the output in the Terminal. Then move it. Put it the line above the for loop. Run it again and check the output in the Terminal.

public class Main {

public static void main(String[] args) {

int a = 2; int b = 4; int c;

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

c = a + b;

a = a + 1;

System.out.print(c + "\n");

}

}

}

B4004A L1 12

Using multiplication *

Change the code a little to make it run the 75 times table. public class Main { public static void main(String[] args) { int a = 0; int b = 75; int c; for(int i=0;i<13;i++) { c = a * b; a = a + 1; System.out.print(c + "\n"); } } }

B4004A L1 13

Concatenating Output

Change the output line of code to this and then look at the output: System.out.print(a + " times " + b + " = " + c + "\n"); Try and write some code to print a box using the character ‘X’ that prints out the same number of ‘Xs’ across and down.

B4004A L1 14

Working with the if statement;

Try writing this code, changing the value of x to get it to run each statement, and then remove some lines of code so that there are less decisions. Pay attention to the brackets. Finally end up with just an if statement, without else. public class Test {

public static void main(String args[]){

int x = 30;

if( x == 10 ){

System.out.print("Value of X is 10");

}else if( x == 20 ){

System.out.print("Value of X is 20");

}else if( x == 30 ){

System.out.print("Value of X is 30");

}else{

System.out.print("This is else statement");

}

}

}

Summary

This seminar has reinforced the coding principles learned during the corresponding lecture;

This seminar has provided users of IntelliJ with a good understanding of how to manage Java code within IntelliJ Development Environment;

Next session

Workshop 2 – Coding Principles

This workshop covers Operators and Strings

End of Seminar

Note: This recording is for your personal use only and not for further distribution or wider review.

© Pearson College 2013