0% found this document useful (0 votes)
12 views4 pages

Java Lab 1

This document contains a lab sheet prepared by Krishna Kumari Ganga for an Advanced Programming 1 course in the Department of Computer Engineering at Omar Mukhtar University, Libya. The lab sheet includes 14 Java programs covering basic concepts like "Hello World", variable declaration and usage, operators, conditional statements, and switch statements. It provides examples and instructions to help students learn and practice core Java programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views4 pages

Java Lab 1

This document contains a lab sheet prepared by Krishna Kumari Ganga for an Advanced Programming 1 course in the Department of Computer Engineering at Omar Mukhtar University, Libya. The lab sheet includes 14 Java programs covering basic concepts like "Hello World", variable declaration and usage, operators, conditional statements, and switch statements. It provides examples and instructions to help students learn and practice core Java programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Omar Mukhtar University Faculty of Engineering

Dept. of Computer Engineering


Albeida , Libya
Instructor: Mrs. Krishna Kumari Ganga
Advanced Programming 1: Lab sheet I

1. Hello program in JAVA


class Hello{
public static void main(String arg[]) {
System.out.println((“Hello World!”);
}
}

2. Hello program in JAVA using Single line comments


class Hello1
//Hello Students
//Good Morning
//Today we are going to execute our programs using JAVA
//This is our first program in JAVA {
public static void main(String arg[]){
System.out.println((“Hello World!”);
}
}
3. Hello program in JAVA using multiple line comments
class Hello2 {
/*
Hello Students
Good Morning
Today we are going to execute our programs using JAVA
This is our first program in JAVA
*/
public static void main(String arg[]) {
System.out.println((“Hello World!”);
}
}
4. JAVA program to add two numbers
class AddNum {
public static void main(String arg[]){
System.out.println(3+5);
}
}
5. JAVA program to add two numbers using variables of type integer
class AddInt {
public static void main(String arg[]) {
int a=3;
int b=5;
System.out.println(a+b);
}
}
6. program to add two numbers using variables
class AddInt1 {
public static void main(String arg[]) {
int a=3;
int b=5;
a=8;
System.out.println(a+b);
}
}

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


7. program to add two numbers using variables of type float
class AddFloat {
public static void main(String arg[]) {
float a=3.6;
float b=5.3;
System.out.println(a+b);
}
}
Note:-By default any real number is double in java. So it generates an error.
8. program to add two numbers using variables of type double
class AddDouble {
public static void main(String arg[]) {
double a=3.6;
double b=5.3;
System.out.println(a+b);
}
}
Since double takes more memory space we can use float for normal real numbers by keeping ‘f ‘after the
value
9. program to add two numbers using variables of type float
class AddFloat1 {
public static void main(String arg[]) {
float a=3.6f;
float b=5.3f;
System.out.println(a+b);
}
}
10. program using variables of type char
class CharDisp {
public static void main(String arg[]) {
Char c=’A’;
System.out.println(c);
}
}
11. program using type casting
class DemoCast {
public static void main(String arg[]) {
Char c=’A’;
System.out.println((int)c);
}
}
12. program using type casting
class DemoCast1 {
public static void main(String arg[]) {
int num=’A’;
System.out.println((char)num);
}
}
13. program using Unary operator(++, --)
class DemoUnary {
public static void main(String arg[]) {
int a=5;
System.out.println(a);
a++;
System.out.println(a);

int b=9;

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


System.out.println(b);
b--;
System.out.println(b);
}
}
14. program using Unary operator(++, --)
class Conditional {
public static void main(String arg[]) {
int a=5;
int b=9;
int big = ( a > b ) ? a : b;
System.out.println("The biggest number is " +big);
}
}

Conditional Statements
1. program to find whether the given number is odd or even
class OddEven {
public static void main(String arg[]) {
int num=5;
if (num%2==1) {
System.out.println(“The number is even”);
}
Else {
System.out.println(“The number is odd”);
}
}
}
2. program to find the biggest number
class Biggest {
public static void main(String arg[]) {
int num1=5;
int num2=9;
int num3=7;
if (num1>num2 && num1>num3) {
System.out.println(“Num1 is the biggest”);
}
else if (num2>num3) {
System.out.println(“Num2 is the biggest”);
}
else {
System.out.println(“Num3 is the biggest”);
}
}
}
3. program using Switch statement
class DemoSwitch {
public static void main(String arg[]) {
String s=”Hai”;
switch(s) {
case “Hai”:
System.out.println(“Hai”);
break;
case “Hello”:
System.out.println(“Hello”);

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


break;
default:
System.out.println(“Welcome”);
}
}
}
4. program using Switch statement
class DemoSwitch1 {
public static void main(String arg[]) {
int i=5;
switch(i) {
case 1:
System.out.println(“January”);
break;
case 2:
System.out.println(“Febraury”);
break;
case 3:
System.out.println(“March”);
break;
case 4:
System.out.println(“April”);
break;
case 5:
System.out.println(“May”);
break;
case 6:
System.out.println(“June”);
break;
case 7:
System.out.println(“July”);
break;
case 8:
System.out.println(“August”);
break;
case 9:
System.out.println(“September”);
break;
case 10:
System.out.println(“October”);
break;
case 11:
System.out.println(“November”);
break;
case 12:
System.out.println(“December”);
break;
default:
System.out.println(“Not a valid number”);
}
}
}

Best of luck

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga

You might also like