Lab Activity 1 121CCS-3 Introduction to programming
Semester II, Year 1441 AH - 2020 AD
Student's Name: Date:
Student's Id # Section #
Topics: Java Basics.
Objectives:
To understand the concept of output statement, comments, datatypes, Scanner class, type casting, and
mathematical expression.
Exercise 1: A program to demonstrate "System.out.println" with two messages:
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World" + " I am new to Java");
}
}
Exercise 2: A program to demonstrate different datatypes, giving values, and printing variables.
public class DataTypesExamples{
public static void main(String []args){
int n1;
n1 = 7; // Assignment
float n2 = 3.85f; //Initialization
double result=1469.05962;
final double Pi=3.1415926535; //constant
char q='?';
String w="What";
System.out.println("n1=" + n1);
System.out.println(n2);
System.out.println(result);
System.out.println(Pi);
System.out.println(w + q);
}
}
121CCS-3 INTRODUCTION TO PROGRAMMING 1
Lab Activity 1 121CCS-3 Introduction to programming
Semester II, Year 1441 AH - 2020 AD
Student's Name: Date:
Student's Id # Section #
Exercise 3: A program to demonstrate getting inputs using "Scanner" class.
import java.util.Scanner; // import the Scanner class
public class Main{
public static void main(String []args){
Scanner myNo = new Scanner(System.in);
int no1;
float no2;
System.out.println("Please enter two numbers:");
no1 = myNo.nextInt();
no2 = myNo.nextFloat();
System.out.println(no1 + no2);
}
}
Exercise 4: A program to demonstrate type casting.
public class Main{
public static void main(String []args){
int bsr;
float balance = 2597.75f;
bsr = (int) balance; //conversion
System.out.println("Your account in SR is " + bsr);
}
}
Lab Activity for the students:
Exercise 5: Write a Java program to define an integer and a float numbers, and use assignment and
initialization to give values. Then, print the variables. (Marks: 0.5)
Exercise 6: Write a Java program to input three integers from user and calculate the product of the numbers.
(Marks: 0.5)
121CCS-3 INTRODUCTION TO PROGRAMMING 2