Untitled 15
Untitled 15
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
int Num, Shift;
char Choice;
Scanner S = new Scanner(System.in);
System.out.print("Enter value:");
Num = S.nextInt();
while(true){
System.out.print("Press l for left shift, press r for right:");
Choice = S.next().charAt(0);
System.out.print("Enter how much to shift:");
Shift = S.nextInt();
if(Choice == 'l'){
Num = Num<<Shift;
System.out.println("The number is now:"+Num+"\
tbinary:"+Integer.toBinaryString(Num));
}
else{
Num = Num>>Shift;
System.out.println("The number is now:"+Num+"\
tbinary:"+Integer.toBinaryString(Num));
}
}
}
}
Output:
[ashutosh@archlinux Exp1]$ javac ShiftOperators.java
[ashutosh@archlinux Exp1]$ java Prog
Enter value:43
Press l for left shift, press r for right:r
Enter how much to shift:2
The number is now:10 binary:1010
Press l for left shift, press r for right:l
Enter how much to shift:5
The number is now:320 binary:101000000
Press l for left shift, press r for right:
Experiment 1
Demonstrate math functions
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
float Num;
Scanner S = new Scanner(System.in);
System.out.print("Enter number:");
Num = S.nextFloat();
System.out.println("The sin of " + Num + "
is:"+Math.sin(Num));
}
}
Output:
[ashutosh@archlinux Exp1]$ java Prog
Enter number:3.14
The sin of 3.14 is:0.0015925480124451862
The cos of 3.14 is:-0.99999873189461
The floor of 3.14 is:3.0
The ceil of 3.14 is:4.0
The root of 3.14 is:1.7720045442673602
Experiment 1
Print the pattern
1
22
333
4444
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
for(int i = 1; i<5; i++){
for(int j = 1; j<=i; j++){
System.out.print(""+i);
}
System.out.print("\n");
}
}
}
Output:
[ashutosh@archlinux Exp1]$ java Prog
1
22
333
4444
Experiment 1
Print the pattern
ABCDE
ABCD
ABC
AB
A
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
for(int i = 5; i>=0; i--){
char Out = 'A';
char Offset = (char)(1);
}
Output:
[ashutosh@archlinux Exp1]$ java Prog
ABCDE
ABCD
ABC
AB
A
Experiment 2:
Read a number from CLI and check if it is an armstrong number or
not.
import java.lang.*;
class Prog{
public static void main(String Args[]){
int Num = Integer.parseInt(Args[0]);
int Sum = 0;
int Len = Args[0].length();
for(int i = 0; i<Len; i++){
int CurrNum = Integer.parseInt("" +
Args[0].charAt(i));
Sum = Sum + (int) Math.pow(CurrNum, Len);
}
if(Sum == Num){
System.out.println("Yes");
return;
}
System.out.println("No");
return;
}
}
Output:
[ashutosh@archlinux Exp2]$ java Prog 32
No
[ashutosh@archlinux Exp2]$ java Prog 1634
Yes
Experiment 2:
Write a program to take a number as input from command line
argument and display first multiple of that number in the range of
100 to 200.
import java.lang.*;
class Prog{
public static void main(String Args[]){
int Number = Integer.parseInt(Args[0]);
int Temp = 0;
if(Number>100 || Number<1){
System.out.println("Invalid number");
return;
}
for(int i = 1; Temp<100; i++){
Temp = Number*i;
}
System.out.println(""+Temp);
}
}
Output:
[ashutosh@archlinux Exp2]$ java Prog 32
128
[ashutosh@archlinux Exp2]$ java Prog 102
Invalid number
[ashutosh@archlinux Exp2]$
Experiment 3:
Write a program to display first N elements of Fibonacci Series
and N should be input from user through keyboard.
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
Scanner S = new Scanner(System.in);
System.out.print("Enter number of numbers:");
int Len = S.nextInt();
int Last, LastM1;
Last = 1;
LastM1 = 0;
System.out.println(""+LastM1);
System.out.println(""+Last);
Experiment 3
write a program to enter a number and check whether that number is
prime number or not.
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
int Num;
Scanner S = new Scanner(System.in);
System.out.print("Number to check:");
Num = S.nextInt();
for(int i = 2; i<=Num/2; i++){
if(Num%i == 0){
System.out.println("No");
return;
}
}
System.out.println("Yes");
return;
}
}
Output:
[ashutosh@archlinux Exp3]$ java Prog
Number to check:1940
No
[ashutosh@archlinux Exp3]$ java Prog
Number to check:1941
No
[ashutosh@archlinux Exp3]$ java Prog
Number to check:43
Yes
Experiment 3
Write a program to find exponents
import java.lang.*;
import java.util.*;
class Power{
public static void main(String Args[]){
int Num;
int Power;
Scanner S = new Scanner(System.in);
System.out.print("Enter number:");
Num = S.nextInt();
System.out.print("Enter power:");
Power = S.nextInt();
}
System.out.println("Result: " + Res);
return;
}
}
Output:
[ashutosh@archlinux Exp3]$ java Power
Enter number:2
Enter power:8
2^8
Result: 256
Experiment 4
Design a class to represent a bank account
import java.lang.*;
import java.util.*;
class Bank{
String AccountName, Type;
int Balance;
void GetData(){
Scanner s = new Scanner(System.in);
System.out.print("Enter name:"); AccountName =
s.nextLine();
System.out.print("Enter type:"); Type = s.nextLine();
System.out.print("Enter balance:"); Balance =
s.nextInt();
}
void Deposit(){
Scanner s = new Scanner(System.in);
System.out.println("Enter ammount to deposit:");
Balance = Balance + s.nextInt();
return;
}
void WithDraw(){
Output:
[ashutosh@archlinux Exp4]$ java Prog
Enter name:Batman
Enter type:ImBatman
Enter balance:1000000
Choices:
1.Deposit 2.Withdraw 3.Display 4.Exit
Choice:1
Enter ammount to deposit:
10000
Choices:
1.Deposit 2.Withdraw 3.Display 4.Exit
Choice:3
Name: Batman
Type: ImBatman
Balance: 1010000
Choices:
1.Deposit 2.Withdraw 3.Display 4.Exit
Choice:2
Enter ammount to deposit:
448
Choices:
1.Deposit 2.Withdraw 3.Display 4.Exit
Choice:3
Name: Batman
Type: ImBatman
Balance: 1009552
Choices:
1.Deposit 2.Withdraw 3.Display 4.Exit
Choice:
Experiment 5
Demonstrate constructor overloading
import java.lang.*;
import java.util.*;
class Car{
String Maker="Default", Color = "BoringGray";
int Driven = 0, Year = 2000;
Car(String pMaker, String pColor, int pDriven, int pYear){
Maker = pMaker;
Color = pColor;
Driven = pDriven;
Year = pDriven;
}
Car(Car pCar){
Maker = pCar.Maker;
Color = pCar.Color;
Driven = pCar.Driven;
Year = pCar.Year;
}
Car(){
super();
}
void PrintInformation(){
System.out.println("\tMaker: "+Maker+"\n\
tColor:"+Color+"\n\tDriven:"+Driven+"\n\tYear:"+Year);
return;
} }
class Prog{
public static void main(String Args[]){
Car Ride;
System.out.println("Using default constructor:");
Ride = new Car();
Ride.PrintInformation();
System.out.println("Using parameterized constructor:");
Ride = new Car("CarMaker", "NotBoringGray", 100, 2012);
Ride.PrintInformation();
System.out.println("Using copy constructor");
Car TempCar = new Car("TempMaker", "TempColor", 20000,
1999);
Ride = new Car(TempCar);
Ride.PrintInformation();
return;
}
}
Output:
[ashutosh@archlinux Exp5]$ java Prog
Using default constructor:
Maker: Default
Color:BoringGray
Driven:0
Year:2000
Using parameterized constructor:
Maker: CarMaker
Color:NotBoringGray
Driven:100
Year:100
Using copy constructor
Maker: TempMaker
Color:TempColor
Driven:20000
Year:20000
Experiment 5
Demonstrate method overrriding
import java.lang.*;
import java.util.*;
class Shape{
void Area(float r){
System.out.println("Area is:"+(3.14*r*r));
return;
}
void Area(int b, int h){
System.out.println("Area is:"+(b*h));
return;
}
void Area(double b, double h){
System.out.println("Area is:"+(b*h/2));
return;
}
}
class Prog{
public static void main(String Args[]){
Shape Shp = new Shape();
Scanner S = new Scanner(System.in);
System.out.print("Enter radius:");
float Radius = S.nextFloat();
Shp.Area(Radius);
System.out.print("Enter width:");
int w = S.nextInt();
System.out.print("Enter height:");
int h = S.nextInt();
Shp.Area(w, h);
System.out.print("Enter width:");
double wt = S.nextDouble();
System.out.print("Enter height:");
double ht = S.nextDouble();
Shp.Area(wt, ht);
return;
}
}
Ouput:
[ashutosh@archlinux Exp5]$ java Prog
Enter radius:32
Area is:3215.36
Enter width:9
Enter height:9
Area is:81
Enter width:43
Enter height:2
Area is:43.0
Experiment 7:
Write a program do input a matrix and find it’s transpose
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
int Size = Integer.parseInt(Args[0]);
int Arr[][] = new int[Size][Size];
Scanner S = new Scanner(System.in);
for(int i = 0; i<Size; i++){
for(int j = 0; j<Size; j++){
System.out.print("Enter value of element
["+i+"]["+j+"]:");
Arr[i][j] = S.nextInt();
}
}
for(int i = 0; i<Size; i++){
for(int j = 0; j<Size; j++){
System.out.print(""+Arr[i][j]+"\t");
}
System.out.println("");
}
System.out.println("Original: \n");
for(int i = 0; i<Size; i++){
for(int j = 0; j<Size; j++){
if(i<j){
Arr[i][j] = Arr[i][j]+Arr[j][i];
Arr[j][i] = Arr[i][j]-Arr[j][i];
Arr[i][j] = Arr[i][j]-Arr[j][i];
}
}
}
System.out.println("Transposed: \n");
for(int i = 0; i<Size; i++){
for(int j = 0; j<Size; j++){
System.out.print(""+Arr[i][j]+"\t");
}
System.out.println("");
}
}
}
Output:
[ashutosh@archlinux Exp7]$ java Prog 3
Enter value of element [0][0]:6
Enter value of element [0][1]:43
Enter value of element [0][2]:62
Enter value of element [1][0]:764
Enter value of element [1][1]:23
Enter value of element [1][2]:65
Enter value of element [2][0]:52
Enter value of element [2][1]:64
Enter value of element [2][2]:739
6 43 62
764 23 65
52 64 739
Original:
Transposed:
6 764 52
43 23 64
62 65 739
Experiment 7:
Write a program to find the number of words in a string
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
System.out.print("\nEnter string:");
Scanner S = new Scanner(System.in);
String str = S.nextLine();
int StrLen = str.length(), Count = 0;
boolean WordStart = false;
for(int i = 0; i<StrLen; i++){
char CurrLetter = str.charAt(i);
if((CurrLetter >= 'a' && CurrLetter <='z') ||
(CurrLetter >= 'A' && CurrLetter <='Z')){
if(WordStart){
continue;
}
WordStart = true;
}
else{
if(WordStart){
Count++;
WordStart = false;
continue;
}
}
}
if(WordStart){
Count++;
}
System.out.println("Number of words:" + Count);
return;
}
}
Output:
[ashutosh@archlinux Exp7]$ java Prog
Enter string:Hello there!
Number of words:2
Experiment 7:
Write a program to find the sum of all rows and columns in a
matrix
import java.lang.*;
import java.util.*;
class Prog{
public static void main(String Args[]){
int Size = Integer.parseInt(Args[0]);
int Arr[][] = new int[Size][Size];
int SumX, SumY;
Scanner S = new Scanner(System.in);
for(int i = 0; i<Size; i++){
for(int j = 0; j<Size; j++){
System.out.print("Enter value of element
["+i+"]["+j+"]:");
Arr[i][j] = S.nextInt();
}
}
for(int i = 0; i<Size; i++){
for(int j = 0; j<Size; j++){
System.out.print(""+Arr[i][j]+"\t");
}
System.out.println("");
}
System.out.println("Original: \n");
for(int i = 0; i<Size; i++){
SumX = 0;
for(int j = 0; j<Size; j++){
SumX = SumX + Arr[i][j];
}
System.out.println("Sum of elements of row "+i+" is
" + SumX);
}
for(int j = 0; j<Size; j++){
SumY = 0;
for(int i = 0; i<Size; i++){
SumY = SumY + Arr[i][j];
}
System.out.println("Sum of elements of column "+j+"
is " + SumY);
}
}
}
Output:
[ashutosh@archlinux Exp7]$ java Prog 3
Enter value of element [0][0]:62
Enter value of element [0][1]:7536
Enter value of element [0][2]:353
Enter value of element [1][0]:1378
Enter value of element [1][1]:52
Enter value of element [1][2]:524
Enter value of element [2][0]:131
Enter value of element [2][1]:5342
Enter value of element [2][2]:9364
62 7536 353
1378 52 524
131 5342 9364
Original: