18
Seminar: Coding Principles Topic Number: 2 App Design for Business

Seminar 2 coding_principles

Embed Size (px)

DESCRIPTION

s2

Citation preview

Page 1: Seminar 2 coding_principles

Seminar: Coding Principles

Topic Number: 2

App Design for Business

Page 2: Seminar 2 coding_principles

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;

Page 3: Seminar 2 coding_principles

3

Open IntelliJ, Create New Project

Page 4: Seminar 2 coding_principles

B4004A L1 4

Select ‘Java’

Page 5: Seminar 2 coding_principles

B4004A L1 5

Select as Screenshot

Page 6: Seminar 2 coding_principles

B4004A L1 6

Add a Project name

Page 7: Seminar 2 coding_principles

B4004A L1 7

Opens Class File: Main.java

Page 8: Seminar 2 coding_principles

B4004A L1 8

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

This is the ‘run’ button:

Page 9: Seminar 2 coding_principles

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.

Page 10: Seminar 2 coding_principles

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

Page 11: Seminar 2 coding_principles

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");

}

}

}

Page 12: Seminar 2 coding_principles

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"); } } }

Page 13: Seminar 2 coding_principles

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.

Page 14: Seminar 2 coding_principles

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");

}

}

}

Page 15: Seminar 2 coding_principles

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;

Page 16: Seminar 2 coding_principles

Next session

Workshop 2 – Coding Principles

This workshop covers Operators and Strings

Page 17: Seminar 2 coding_principles

End of Seminar

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

© Pearson College 2013

Page 18: Seminar 2 coding_principles