0% found this document useful (0 votes)
40 views12 pages

Week 2

The document outlines the topics covered in week 2 of a Java lecture series, including: - Printing "hello world" in Java and the structure of a basic Java program - Scalar variable types like int, float, char, and boolean - Operators, strings, arrays, control flow, classes and objects, and creating/initializing objects - Copy constructors and basic input/output in Java using classes like Console and Scanner

Uploaded by

Kylo Ren
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)
40 views12 pages

Week 2

The document outlines the topics covered in week 2 of a Java lecture series, including: - Printing "hello world" in Java and the structure of a basic Java program - Scalar variable types like int, float, char, and boolean - Operators, strings, arrays, control flow, classes and objects, and creating/initializing objects - Copy constructors and basic input/output in Java using classes like Console and Scanner

Uploaded by

Kylo Ren
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/ 12

Week-2

Lecture-1

Lecture-2

Lecture-3

Lecture-4 Programming Concepts Using Java


Lecture-5
Week 2 Revision
Getting started

Week-2

Java program to print hello, world


Lecture-1 public class HelloWorld{
Lecture-2 public static void main(String[] args) {
Lecture-3 System.out.println("hello, world);
Lecture-4
}
Lecture-5
}
A Java program is a collection of classes
All code in Java lives within a class
Modifier public specifies visibility
The signature of main( )
Input parameter is an array of strings; command line arguments
No output, so return type is void
Write once, run anywhere
Scalar types

Week-2

Java has eight primitive scalar types


Lecture-1
int, long, short, byte
Lecture-2
float, double
Lecture-3
char
Lecture-4
boolean
Lecture-5 We declare variables before we use them
int x, y;
x = 5;
y = 10;
Characters are written with single-quotes (only)
char c = ‘x’;
Boolean constants are true, false
boolean b1, b2;
b1 = false;
b2 = true;
Scalar types

Week-2

Initialize at time of declaration


Lecture-1 flat pi = 3.1415927f;
Lecture-2
Modifier final indicates a constant
Lecture-3

Lecture-4
final float pi = 3.1415927f;
Lecture-5
Operators

Week-2

Arithmetic operators are the usual ones


Lecture-1 +, -, *, /, %
Lecture-2
No separate integer division operator //
Lecture-3

Lecture-4
When both arguments are integer, / is integer division
Lecture-5 No exponentiation operater, use Math.pow()
Math.pow(a,n) returns an
Special operators for incrementing and decrementing integers
int a = 0, b = 10;
a++; // Same as a = a+1
b--; // Same as b = b-1
Shortcut for updating a variable
int a = 0, b = 10;
a += 7; // Same as a = a+7
Strings

Week-2

String is a built-in class


Lecture-1 String constants enclosed in double quotes
Lecture-2
String s = "Hello", t = "world";
Lecture-3

Lecture-4
+ is overloaded for string concatenation
Lecture-5 String s = "Hello";
String t = "world";
String u = s + " " + t;
// "Hello world"
Strings are not arrays of characters
Instead use s.charAt(0), s.substring(0,3)
Arrays

Week-2

Arrays are also objects


Lecture-1 Typical declaration
Lecture-2
int[] a;
Lecture-3
a = new int[100];
Lecture-4

Lecture-5 Or int a[] instead of int[] a


a.length gives size of a
Array indices run from 0 to a.length-1
Control flow

Week-2

Conditional execution
Lecture-1 if (condition) { ... } else { ... }
Lecture-2
Conditional loops
Lecture-3

Lecture-4
while (condition) { ... }
Lecture-5 do { ... } while (condition)
Iteration - Two kinds of for
Multiway branching – switch
Classes and objects

Week-2

A class is a template for an encapsulated type


Lecture-1 An object is an instance of a class
Lecture-2
public class Date {
Lecture-3
private int day, month, year;
Lecture-4
public Date(int d, int m, int y){
Lecture-5
day = d;
month = m;
year = y;
}
public int getDay(){
return(day);
}
}
Instance variables - Each concrete object of type Date will have local copies of date,
month, year
Creating and initializing objects

Week-2

new creates a new object


Lecture-1 How do we set the instance variables?
Lecture-2 Constructors — special functions called when an object is created
Lecture-3
Function with the same name as the class
Lecture-4
d = new Date(13,8,2015);
Lecture-5
Constructor overloading - same name, different signatures
A constructor can call another one using this
If no constructor is defined, Java provides a default constructor with empty arguments
new Date() would implicitly invoke this
Sets instance variables to sensible defaults
For instance, int variables set to 0
Only valid if no constructor is defined
Otherwise need an explicit constructor without arguments
Copy constructors

Week-2

Create a new object from an existing one


Lecture-1
public class Date {
Lecture-2 private int day, month, year;
Lecture-3 public Date(int d, int m, int y){
Lecture-4 day = d; month = m; year = y;
Lecture-5 }
public Date(Date d){
this.day = d.day; this.month = d.month; this.year = d.year;
}
}
public class UseDate() {
public static void main(String[] args){
Date d1,d2;
d1 = new Date(12,4,1954); d2 = new.Date(d1);
}
}
Basic input and output in java

Week-2

Reading input
Lecture-1
Use Console class
Lecture-2
Use Scanner class
Lecture-3 Scanner in = new Scanner(System.in);
Lecture-4 String name = in.nextLine();
Lecture-5
int age = in.nextInt();

You might also like