0% found this document useful (0 votes)
41 views

CS175 Lecture 3

This document summarizes Lecture 3 of the CS175: Programming in Java course. It covers types, variables, operators, and basic Java programs. The key topics discussed include: 1. The basic types in Java - boolean, int, double, String. 2. How variables are declared and used to store values of a particular type. 3. Common operators like assignment, addition, subtraction, multiplication, and division. 4. The order of operations and how expressions are evaluated in Java. 5. Examples of simple Java programs that output text and perform basic calculations.

Uploaded by

Pasiano Nemes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

CS175 Lecture 3

This document summarizes Lecture 3 of the CS175: Programming in Java course. It covers types, variables, operators, and basic Java programs. The key topics discussed include: 1. The basic types in Java - boolean, int, double, String. 2. How variables are declared and used to store values of a particular type. 3. Common operators like assignment, addition, subtraction, multiplication, and division. 4. The order of operations and how expressions are evaluated in Java. 5. Examples of simple Java programs that output text and perform basic calculations.

Uploaded by

Pasiano Nemes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CS175: Programming in Java

Lecture 3

Dr. Juma Lungo


[email protected]

Types, Variables, Operators

l  Goal
•  Learn enough Java to do something useful
•  Examples:
•  Perform some arithmetic
•  Solve quadratic equation

1
About a Computer

Memory l  CPU Instructions


l  c = a + b

Central
Processing •  Read location a
Unit (CPU)
•  Read location b
•  Add
•  Write to location c
Input/Output
(IO Devices)

Compiling Java

Source Code javac Byte Code java


(.java) (.class)

2
First Java Program

Program Structure

3
System Output

System.out.println(some String) outputs


to the console/terminal

Example:
System.out.println(“output”);

Second Program

4
Quiz 1

l  Write a Java Program to output the following


sentence “Mama Land”

ma La nd”);
…… in tln ( “ M a
ut.pr
System.o
……

Types

l  Kinds of values that can be stored and


manipulated
• boolean: Truth value (true or false).
• int: Integer (0, 1, -47).
• double: Real number (3.14, 1.0, -2.1).
• String: Text (“hello”, “example”).

5
Variables

l  Namedlocation that stores a value of


one particular type.

• Form: TYPE NAME;


l  Example: String test;

Quiz 2: List the (i) Types and (ii)


Variables used in this program
Class Quiz2
int coursenumber;
double test1, test2, total; Types:
string remarks; int
Public static void main (String args[]) double
{ string
total = test1 + test2;
System.out.println(total); Variables:
if total > 40{ coursenumber
remaks = pass; test1
}else{ test2
remaks = Failue; total
} remarks
System.out.println(remarks);
}

6
Operators

Symbols that perform simple computations

1. Assignment: =
2. Addition: +
3. Subtraction: -
4. Multiplication: *
5. Division: /

Order of Operations (BODMAS)


BODMAS is an acronym and it stands for
•  Bracket,
•  Of,
•  Division,
•  Multiplication,
•  Addition and
•  Subtraction.
It explains the order of operations to solve
an expression.

7
Order of Operations

Follows standard math rules:


1.  Parentheses
2.  Multiplication and division
3.  Addition and subtraction

Assignment

Use = to give variables a value.

Example:
String remarks;
remarks = “Passed Java Test1”;
----
System.out.println(remarks) ;

8
Assignment

Can be combined with a variable


declaration.

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

String Concatenation (+)


Use + to concatenate string

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;

l  What is the output of this Program?


……
System.out.println(c);

Practical Questions 1

l  Write a Java Program to print “College of


ICT” 7 times.

UDSM

12
Practical Questions 2

l  Write a Java Program to solve the area of a


a triangle with base = 8cm and height =
15cm

UDSM

Practical Questions 3

Write a Java Program to solve for


x: x2-3x-10 = 0

UDSM

13

You might also like