Wa0002.
Wa0002.
Source code:
import java.util.*;
public class Program1
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter two integers to perform arithmetic progression");
System.out.print("Enter integer 1 : ");
int a = scan.nextInt();
System.out.print("Enter integer 2 : ");
int b = scan.nextInt();
System.out.println("Performing arithmetic progression");
System.out.println("Addition : "+(a+b));
System.out.println("Subtraction : "+(a-b));
System.out.println("Multiplication : "+(a*b));
System.out.println("Division : "+(a/b));
System.out.println("Modulus : "+(a%b));
}
}
Output:
Program 2: Concatenation
Source code:
import java.util.*;
public class Program2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Concatenating three different strings");
System.out.print("String 1 : ");
String a = scan.nextLine();
System.out.print("String 2 : ");
String b = scan.nextLine();
System.out.print("String 3 : ");
String c = scan.nextLine();
System.out.println("The concatenated string is : "+a+" "+b+" "+c);
}
}
Output:
Concatenating three different strings
String 1 : We
String 2 : Are
String 3 : Venom
The concatenated string is : We Are Venom
22-UCA-039
Source Code:
import java.util.Scanner;
public class TemperatureConvert
{
public static void main(String[] args)
{
Scanner T = new Scanner(System.in);
System.out.print("Enter value for Fahrenheit :");
float Fahren = T.nextFloat();
System.out.println("The given Fahrenheit is : "+Fahren);
float Celsi = (Fahren-32)*5/9;
System.out.println("Fahrenheit in Celsius is : "+Celsi);
}
}
Output:
Source Code:
import java.util.Scanner;
public class PosNegZero
{
public static void main(String[] args)
{
Scanner A = new Scanner(System.in);
System.out.print("Enter your integer number here : ");
int evalu = A.nextInt();
if (evalu>0)
{
System.out.println(evalu+" is a positive integer");
}
else if (evalu==0)
{
System.out.println(evalu+" is zero");
}
else
{
System.out.println(evalu+" is a negative integer");
}
}
}
Output:
U:\cas39\Java Programming>java PosNegZero
Enter your integer number here : 45
45 is a positive integer
22-UCA-039
Source code:
import java.util.Scanner;
public class CountOddEven2
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the start of the range : ");
int start = scanner.nextInt();
System.out.print("Enter the end of the range : ");
int end = scanner.nextInt();
int oddCount = 0;
int evenCount = 0;
for (int i = start; i<=end; i++)
{
if (i%2==0)
{
evenCount++;
}
else
oddCount++;
}
System.out.println("Count of even numbers in the given range is : "
+evenCount);
System.out.println("Count of odd numbers in the given range is : "
+oddCount);
}
}
Output:
Program 6: Multiples of 11
Source code:
/* multiples of 11 using
while, do while and for loop */
import java.util.Scanner;
public class MultiplesOf11
{
public static void main(String[] args)
{
int x = 0;
Scanner obj = new Scanner(System.in);
System.out.println("Welcome User, let\'s print the multiples of 11!");
System.out.print("Enter the start of the range : ");
int start = obj.nextInt();
System.out.print("Enter the end of the range : ");
int end = obj.nextInt();
System.out.println("WHILE LOOP");
while(x<=end)
{
if (x%11==0 && x!=0)
{
System.out.print(x+"\t");
}
x++;
}
System.out.println("\n*FOR LOOP*");
for (int i = start; i <= end; i++)
{
if (i%11==0)
System.out.print(i + "\t");
}
}
22-UCA-039
Output:
Source code:
import java.util.Scanner;
public class Program7
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
System.out.print("Enter a single digit positive number : ");
int a = obj.nextInt();
switch (a)
{
default:
System.out.println("Error!, please enter a single digit positive number");
break;
case 0:
System.out.println("Zero");
break;
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
case 5:
System.out.println("Five");
break;
case 6:
System.out.println("Six");
break;
case 7:
System.out.println("Seven");
break;
case 8:
System.out.println("Eight");
break;
case 9:
22-UCA-039
System.out.println("Nine");
break;
}
}
}
Output:
Source code:
import java.util.Scanner;
public class Program8
{
static void Arm_Num(int x)
{
int y=x, a=0;
while (y!=0)
{
int r = y%10;
a = a+(r*r*r);
y = y/10;
}
if(a==x)
{
System.out.println("The given number is an Armstrong number");
}
else
{
System.out.println("The given number is not an Armstrong number");
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter a number to chech it is an Armstrong number or
not : ");
int x = s.nextInt();
Arm_Num(x);
}
}
Output:
Source code:
Output:
Source code:
import java.util.Scanner;
public class Program10
{
static int isOdd(int a)
{
if (a%2!=0)
{
return 1;
}
else
{
return 0;
}
}
static int isMultiple5(int a)
{
if (a%5==0)
{
return 1;
}
else
{
return 0;
}
}
static int Add(int a, int b)
{
return a+b;
}
static boolean inRange(int a, int b, int c)
{
if (b<=a && a<=c)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter number to be checked odd : ");
int a = s.nextInt();
System.out.println(isOdd(a));
22-UCA-039
Output:
Source code:
import java.util.Scanner;
public class Program11
{
public static void main(String[] args)
{
int[] nos = {1,2,3,4,5};
String[] vehicles = {"Cycle","Car","Bike","Truck"};
float[] temp = {28.2f,30.1f,33.5f,66.7f};
Output:
U:\cas39\Java Programming\Observation Programs>java Program11
1
2
3
4
5
Cycle
Car
Bike
Truck
28.2
30.1
33.5
66.7
22-UCA-039
22-UCA-039
Source code:
import java.util.Scanner;
public class Program12
{
static int sum(int a, int b)
{
return a+b;
}
System.out.println(sum(a,b));
System.out.println(sum(x,y));
22-UCA-039
System.out.println(sum(m,n));
}
}
Output:
Enter int numbers
Enter int number 1 : 344
Enter int number 2 : 342
Enter float numbers
Enter float number 1 : 3453.34
Enter float number 2 : 4534.23
Enter double numbers
Enter double number 1 : 23423.2342
Enter double number 2 : 23423.4545
686
7987.5703
46846.6887
22-UCA-039
Source code:
// Program 13 - CLASSES AND OBJECTS - 2
import java.util.Scanner;
public class Employee
{
String emp_name;
int emp_id, emp_basicpay, emp_hra;
double emp_pf, emp_netsal;
public void accept(String name, int id, int basicpay, int hra)
{
emp_name = name;
emp_id = id;
emp_basicpay = basicpay;
emp_hra = hra;
}
private double calcpf()
{
emp_pf = 0.12 * emp_basicpay;
return emp_pf;
}
public double calcnet()
{
emp_netsal = (emp_basicpay + emp_hra) - emp_pf;
return emp_netsal;
}
public void display()
{
System.out.println("Employee name : "+emp_name);
System.out.println("ID : "+emp_id);
System.out.println("Basic pay : "+emp_basicpay);
System.out.println("HRA : "+emp_hra);
System.out.println("Provident fund : "+emp_pf);
System.out.println("Net salary : "+emp_netsal);
}
Output:
Enter the name of the employee : Lee jinhyuk
Enter id : 1001
Basic pay : 10000000
HRA : 900000
Employee name : Lee jinhyuk
ID : 1001
Basic pay : 10000000
HRA : 900000
Provident fund : 1200000.0
Net salary : 9700000.0
22-UCA-039
Source code:
System.out.println("mark 2 : "+mark2);
System.out.println("mark 3 : "+mark3);
System.out.println("Total marks : "+total_marks);
System.out.println("Average marks : "+avg_marks);
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter student name : ");
String name = s.nextLine();
System.out.print("Enter student id : ");
int id = s.nextInt();
System.out.println("Enter three marks");
System.out.print("Mark 1 : ");
int m1 = s.nextInt();
System.out.print("Mark 2 : ");
int m2 = s.nextInt();
System.out.print("Mark 3 : ");
int m3 = s.nextInt();
Student obj = new Student();
obj.accept(name,id,m1,m2,m3);
obj.calctotal();
obj.calcavg();
obj.display();
}
}
Output:
Enter student name : Jin Sung
Enter student id : 1021
Enter three marks
Mark 1 : 100
Mark 2 : 99
Mark 3 : 100
Student name : Jin Sung
Student ID : 1021
mark 1 : 100
mark 2 : 99
mark 3 : 100
Total marks : 299
Average marks : 99
22-UCA-039
Source code:
// Program 15 - Types of Constructors
class cons
{
int x,y;
public cons()
{
x = 100;
y = 30;
}
public cons(int a, int b)
{
22-UCA-039
x = a;
y = b;
}
public static void main(String[] args)
{
cons obj = new cons();
System.out.println("Value of x and y is : "+obj.x+" "+obj.y);
cons obj2 = new cons(17,27);
System.out.println("Value of x and y : "+obj2.x+" "+obj2.y);
}
}
Output:
U:\cas39\Java Programming\Observation Programs>java cons
Value of x and y is : 100 30
Value of x and y : 17 27
Source code:
import java.util.Scanner;
class Person
22-UCA-039
{
Scanner s = new Scanner(System.in);
String fname, lname;
public void getFirstName()
{
System.out.print("Enter first name : ");
fname = s.nextLine();
}
public void getLastName()
{
System.out.print("Enter last name : ");
lname = s.nextLine();
}
}
class Program16
{
public static void main(String[] args)
{
employee obj = new employee();
obj.getFirstName();
obj.getLastName();
obj.getJobTitle();
System.out.println("Printing all the collected information...");
System.out.println("Full name : "+obj.fname+" "+obj.lname);
System.out.println("Job title : "+obj.jobtitle);
}
}
Output:
Source code:
abstract class Testabs
{
abstract void m1();
abstract void m2();
public void m3()
{
System.out.println("Method 3");
}
public void m4()
{
System.out.println("Method 4");
}
}
class Program17
{
public static void main(String[] args)
{
Test obj = new Test();
obj.m1();
obj.m2();
obj.m3();
obj.m4();
}
}
Output:
Source Code:
import java.util.*;
interface inter1
{
void GetStudentNames();
}
interface inter2
{
void DisplayStudentNames();
}
class Program18
{
public static void main(String[] args)
{
Impletest obj = new Impletest();
obj.GetStudentNames();
obj.DisplayStudentNames();
}
}
22-UCA-039
Output:
Source Code:
class Game
{
void Stats()
{
System.out.println("Health points");
}
}
Output:
Health points
Magic points
Experience points
22-UCA-039
Source Code:
// Program 20 - PACKAGES
package MyPackageIsBIG;
import java.util.*;
public class MyMethods
{
static void drawEqualTo()
{
System.out.println("==================================
=================");
}
static void EnterName()
{
String a,b;
Scanner BigD = new Scanner(System.in);
System.out.println("Enter your name");
System.out.print("Enter first name : ");
a = BigD.nextLine();
System.out.print("Enter last name : ");
b = BigD.nextLine();
System.out.println("Welcome, "+a+" "+b);
}
public static void main(String[] args)
{
drawEqualTo();
EnterName();
drawEqualTo();
}
}
Output:
Source Code:
PACKAGE CODE:
package testpack;
public class mypack
{
int a=10;
int b=5;
}
}
SOURCE CODE:
import testpack.*;
public class Ex_21
{
public static void main(String[] args)
{
testpack.mypack.addition();
testpack.mypack.divide();
}
}
OUTPUT:
U:\cas39\java>javac mypack.java
U:\cas39\java>javac -d . mypack.java
U:\cas39\java>java testpack.mypack
22-UCA-039
inside package:
15
2
22-UCA-039
Source Code:
OUTPUT:
U:\cas39\java>javac Program23.java
U:\cas39\java>java Program23
given index is out of bounds
22-UCA-039
Source Code:
import java.util.*;
class myerror extends Exception
{
public myerror(String message)
{
super(message);
}
}
class test
{
public void display() throws myerror{
throw new myerror("HELP ME, SOMEONE");
}
}
class error
{
public static void main(String[] args)
{
try
{
test obj=new test();
obj.display();
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUM TO DIVIDE:");
int num1=sc.nextInt();
System.out.println("ENTER THE NUM TO DIVIDE:");
int num2=sc.nextInt();
System.out.println("ANSWER:"+num1/num2);
}
catch(ArithmeticException a)
{
System.out.println("ERROR, CAN'T ABLE TO DIVIDE");
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("END OF THE WORLD");
}
}
}
22-UCA-039
OUTPUT:
Source Code:
Output:
Source Code:
OUTPUT: