Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Week 2 Object Oriented

Programming

Lab session 2
Learning I/O operations in Java console.
Exploring Java’s built in classes.
Prepared BY: Engr. Javeria Barkat

 
1
Reference: Liang, Y., 2015. Intro To Java Programming. 10th ed. Pearson Education Limited .
Objectives
• Unit 2.1 Basics Data types
• Unit 2.2 Variables and Type casting
• Unit 2.3 Input/ Output from Console In Java
• Unit 2.4 Java Math Class Features with concatenation
• Unit 2.5 Java Built in Date Class

2
Unit 2.1 Basics Concepts Data types declaration in JAVA

3
Data Types in Java
• A data type in a programming language is a set of data with values having predefined characteristics.
• There are two basic types in Java.
1. Primitive
Primitive Data Types There are eight primitive data types supported by Java. Primitive data types are
predefined by the language and named by a key word. Primitive types are not objects; so does not have
any methods for further processing.
2. Non-Primitive
It is a reference data type, which are references to objects. Types are
• Classes
• Interfaces
• Arrays

4
5
Data types Representation In Java

6
Like as C and C++

7
Unit 2.2 Variables and Type casting

8
What are the Variables
• Variable is a name of memory location.
• It is name of reserved area allocated in memory.

In the given example; int is data type, a is variable name and


10 is the value that a variable holds, followed by a terminator;
 

9
Naming Convention of Variables in Java
• Camel Case:

Except for the first letter of the first word, every subsequent
word has its first letter capitalized. For example:
`userAccountType`. Camel Case is generally used for naming
functions, variables, packages etc.

10
Type Conversion/ Casting

• Casting is an operation that converts a value of one data type into a


value of another data type. The syntax for casting a type is to specify the
target type in parentheses, followed by the variable’s name or the value
to be cast.

For example;
System.out.println((int)1.7);

• The above statement displays 1. When a double value is cast into an int
value, the fractional part is truncated.

11
Unit 2.3 Learning Input / Output from Java Console

12
Console input
• System.in to the standard input device.
• Console input is not directly supported in Java,
But;
• Scanner class is used to create an object to read input from System.in,
• Import the Scanner class by adding following utility on top
import java.util.Scanner;
• Scanner class object can be created as follows:
Scanner input = new Scanner(System.in);
double radius = input.nextDouble();
Here, input is scanner class object, radius is a variable that store the double value
entered at runtime on console

13
Console output
• Java uses System.out to refer to the standard output device.
• To perform console output, we can use
• Print method to print primitive value without break the line
System.out.print("Hello ");

• println (namely print line) method is used to display a primitive value or a string to
the console.
System.out.println("world");

14
• Example:
Write a Java program to take different input from user and store the input in
variables with respective data type and then display the data on the console.
import java.util.Scanner;  
public class Input{ 
public static void main(String[] args) {
String name;
char gender;
int age;
double height;

15
Scanner input = new Scanner(System.in);

System.out.println("Enter Name: ");


name = input.nextLine();
System.out.println("Enter Gender(M/F): ");
gender = input.next().charAt(0);
System.out.println("Enter Age: ");
age = input.nextInt();
System.out.println("Enter Height: ");
height = input.nextDouble();

System.out.println(" ______ Student Data ______\n");


System.out.println( name);
System.out.println( );
System.out.println(gender );
System.out.println();
System.out.println( age);
System.out.println( );
System.out.println( height);
}
}

16
Unit 2.4 Java Math Class Features with concatenation

17
String concatenation is combining of two strings or strings with the value
of variables

18
19
Math class file
• Math class file is included for the definitions of math functions listed
below.
• It can execute following methods by including
import java.lang.Math;
• Trignometic / Maths Functions
sin(n) sinh(n)
cos(n) cosh(n)
tan(n) tanh(n)
pow(nmb,pwr) sqrt(n)
20
• Write a Java program to explore Math class

import java.lang.Math;

public class MathClass {


public static void main(String[] args) {
double a=45,b=1,sn,cs,tn,snh,csh,tnh;
sn=Math.sin(a);
cs=Math.cos(a);
tn=Math.tan(a);
 
snh=Math.sinh(b);
csh=Math.cosh(b);
tnh=Math.tanh(b);
 

21
System.out.println("\nTrignometric Functions");
System.out.println("sin 45 = " + sn);
System.out.println("cos 45 =" + cs);
System.out.println("tan 45 =" + tn);
 
System.out.println("\nHyperbolic Functions");
System.out.println("sinh 1 = " + snh);
System.out.println("cosh 1 = " + csh);
System.out.println("tanh 1 = " + tnh);
}
}

22
Unit 2.5 Java Date Class

23
Java Date Class
• Java provides a system-independent encapsulation of date and time in the java.util.Date

class. The no-arg constructor of the Date class can be used to create an instance for the current
date and time.

24
• Write a Java program to explore Date class.
import java.util.Date;
class DateDemo
{
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();
// display time and date using toString()
System.out.println(date);
// Display number of milliseconds since midnight, January 1, 1970
long msec = date.getTime();
System.out.println("Milliseconds since Jan. 1, 1970 " + msec);
}
}

25

You might also like