CS175 Lecture 3
CS175 Lecture 3
Lecture 3
l Goal
• Learn enough Java to do something useful
• Examples:
• Perform some arithmetic
• Solve quadratic equation
1
About a Computer
Central
Processing • Read location a
Unit (CPU)
• Read location b
• Add
• Write to location c
Input/Output
(IO Devices)
Compiling Java
2
First Java Program
Program Structure
3
System Output
Example:
System.out.println(“output”);
Second Program
4
Quiz 1
ma La nd”);
…… in tln ( “ M a
ut.pr
System.o
……
Types
5
Variables
6
Operators
1. Assignment: =
2. Addition: +
3. Subtraction: -
4. Multiplication: *
5. Division: /
7
Order of Operations
Assignment
Example:
String remarks;
remarks = “Passed Java Test1”;
----
System.out.println(remarks) ;
8
Assignment
Example:
double radius = 7;
boolean isJune = true;
Quiz 3
Write down the Outputs of the following Java Program
class Hello3 {
public static void main(String[] arguments) {
String course = "CS 175";
System.out.println(course);
course = "Programming in Java"; CS 175
Programming in Java
System.out.println(course);
}
}
9
Quiz 4: What is the value of (i)
total (ii) remarks
Class Quiz4
double test1 = 28.1;
double test2 = 23.9; double total;
string remarks;
Public static void main (String args[]) Total: 52.0
{
total = test1 + test2;
System.out.println(total); Remarks: Pass
if total > 40{
remaks = pass;
}else{
remaks = Fail;
}
System.out.println(remarks);
}
Quiz 5
10
Quiz 6
For example:
String text = "hello" + " world";
text = text + " number " + 5;
……
ntln(text);
System.out.pri
Output: mber 5
hello world nu
……
11
String Concatenation (+)
Given:
String a = “a”;
String b = “letter b”; Output:
a = “letter a”; letter a and letter b
String c = a + “ and “ + b;
Practical Questions 1
UDSM
12
Practical Questions 2
UDSM
Practical Questions 3
UDSM
13