Program revision 1

Download Report

Transcript Program revision 1

DT249-Information Systems
Research Practice-2015-16
Programming Revision Lecture 1
Lecturer: Patrick Browne
Four main components
• We focuses on 4 main areas;
– Procedural programming,
– Object Oriented Programming with Java at
command prompt,
– Algorithms and data structures,
– Using Eclipse for applications and testing.
Procedural programming.
• What does a programming language do?
Language basics: compilers, interpreters,
variables, primitive data types,
declarations, constants, functions,
operations and procedures, arrays,
queues and other data structures, program
control, simple input and output (I/O) e.g.
keyboard and screen
Object Oriented Programming
• What are objects in a programming and
real world sense? We cover object
oriented concepts such as classes,
objects, methods inheritance, messages,
more complex input and output streams,
files, XML, graphical based I/O.
Algorithms and data structures
• How do we represent and process data?
We look at simple calculations, sorting,
searching.
What is a Programming Language?
• A programming language helps people to
express ideas in a form that can be
understood by a computer.
Figure from: Java Programming for Spatial Sciences by
Jo Wood
Starting
• On your laptop make a folder for this
course called C:\JavaDir. We will call
this your working directory or your working
folder or working directory.
Slides and source code
• Copy and unzip the source code to your
working directory/folder: Source code at:
• http://www.comp.dit.ie/pbrowne/Informatio
n%20Systems%20Research%20Practice/
JavaProgramming.7z
Writing, compiling, running
• Open a command prompt.
• Change directory to your working dir.
cd C:\JavaDir
• Open the file HelloWorldApp.java using
TextPad, Notepad++, or Notepad.
Writing, compiling, running
• In this case we just view the code, rather than write it.
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
} }
• The program HelloWorldApp implements an application that simply prints
"Hello World!" to standard output. Compile with command
>javac HelloWorldApp.java
• This will make a file HelloWorldApp.class
• The program can be run at command prompt with
>java HelloWorldApp
• Get program to print your name.
• In order for a computer to "execute" or "run" a program, the program must
be translated into the ones and zeros that the computer "understands."
Variables
• The Java programming language is a strongly typed
language, which means that every variable and every
expression has a type that is known at compile time.
Types limit the values that a variable can hold or that an
expression can produce, limit the operations supported
on those values, and determine the meaning of the
operations. Strong typing helps detect errors at compile
time.
Variables
• The purpose of variable is to hold a value.
We can say x holds the value 4 as follows
• int x = 4;
• Every variable in Java must have a type.
• We can change the value of x during the
program, as follows:
• x = 3;
• Variables are somewhat like variables in
maths, but there are differences.
Variables
• Variables are somewhat like variables in maths,
but there are differences.
• int x,y,z;
• In the following is similar to maths.
• x = y + z;
• But the next line is not true in maths.
• x = x + 3;
• These are assignments where the variable on
the LHS is made to store the result of the
expression on RHS.
Variables
• Here is a program with a variable, that is
changed during the program.
class VariableApp {
public static void main(String[] args) {
int x = 4;
System.out.println(“x is “ + x);
x = 3;
System.out.println(“x is “+ x); }}
• Run this program.
Variable with wrong type
• For the moment we will insist that x can only be
of type int. So the following is not allowed:
• x = “hello”;
class VariableApp {
public static void main(String[] args) {
int x = 4;
System.out.println(“x is “ + x);
x =“Hello”;
System.out.println("x is "+ x); }}
• Compile and run this program.
• What happens?
Java Primitive Data Types
• The type of value that a variable can hold is called data type. When
we declare a variable we need to specify the type of value it will
hold along with the name of the variable. This tells the compiler that
the particular variable will hold certain amount of memory to store
values. For example, in the code above x is of type int and takes
32 bits to hold the integer value.
Java Primitive Data Types
• Variable declaration and initialization
• int a;
// This is a declaration
• int a = 0; // This is an declaration and initialization
• It is possible to declare a variable without giving it a value.
• // represents the start of a comment
Basic Maths
• Add two number
class AddApp {
public static void main(String[] args) {
int x = 3;
int y = 4;
int result = x + y;
System.out.println("x plus y is " + result
);
}
}
Logical Operators
Logical Operators
class BoolApp {
public static void main(String[] args) {
int waterLevel = 2000;
boolean floodRisk = true;
boolean inFloodPlane = true;
boolean stormEvent = true;
floodRisk = (floodRisk || stormEvent)
&& (waterLevel >= 1000);
System.out.println(“Flood risk " +
floodRisk );
}
}
Sequence, iteration, and condition
• Sequence, iteration, and condition control
the flow of a program.
Sequence, Iteration, and condition
• We have already seen sequence. We say
how one program statement could follow
another. We can also have sequences of
conditions or iterations.
Iteration
class IterApp{
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
System.out.println(“your name");
}
}
Condition (if)
class CondApp {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if(value1 == value2) System.out.println("value1 == value2");
if(value1 != value2) System.out.println("value1 != value2");
if(value1 > value2) System.out.println("value1 > value2");
if(value1 < value2) System.out.println("value1 < value2");
if(value1 <= value2) System.out.println("value1 <= value2");
}
}
Condition (if-else)
class CondApp2 {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
Input
• Takes input from command line.
public class EchoApp {
public static void main (String[] args)
{
for (String s: args) {
System.out.println(s);
}
}}
• Run at the command prompt as:
• > java EchoApp 4 3 A c
Input
import java.io.*;
class InputApp1
{
public static void main (String args[])
throws IOException {
BufferedReader b = new BufferedReader (new InputStreamReader(System.in));
String s;
int i;
System.out.print("Please input a number:");
s = b.readLine();
try{
i = Integer.parseInt(s);
} catch(NumberFormatException NFE){
System.out.println("Please try again");
System.out.println("You entered " + i);} }
•
Enter an non-numeric (e.g. x). What happens?
Input
import java.io.*;
class IntInApp{
public static void main (String args[]) throws IOException {
BufferedReader b = new BufferedReader (new InputStreamReader(System.in));
String string;
int i;
int sum = 0;
System.out.println("Please input numbers:");
System.out.println("Type 0 to exit");
do{
string = b.readLine();
try{
i = Integer.parseInt(string);
} catch(NumberFormatException NFE){
System.out.println("Please try again");
// comment out
i = 0;
}
sum = sum + 1;
System.out.println("Count = "+ sum);
} while(i != 0);} }
Input
import java.util.Scanner;
public class DistanceApp {
public static void main(String[] args){
int x1, y1, x2, y2;
double distance;
Scanner scan = new Scanner (System.in);
System.out.print("Enter the x coordinate for point 1: ");
x1 = scan.nextInt();
System.out.println("The x coordinate for point 1: "+ x1);
System.out.print("Enter the y coordinate for point 1: ");
y1 = scan.nextInt();
System.out.println("The y coordinate for point 1: "+ y1);
System.out.print("Enter the x coordinate for point 2: ");
x2 = scan.nextInt();
System.out.println("The x coordinate for point 2: "+ y1);
System.out.print("Enter the y coordinate for point 2: ");
y2 = scan.nextInt();
System.out.println("The y coordinate for point 2: "+ y2);
distance = Math.sqrt( Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
System.out.println("The distance between the two points is " + distance);
}}
Change to floating point input using nextDouble and change coord to double.