0% found this document useful (0 votes)
21 views

Comp Prac 12th

Uploaded by

vihaanjog2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Comp Prac 12th

Uploaded by

vihaanjog2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

12 GRADE

TH

COMPUTER SCIENCE PRACTICAL FILE

Name: Vihaan Jog


Class:
12
Division:
B Roll
no:13
INDEX
SR. NO TITLE REMARKS

1 Term 2 Questions

1. Armstrong
2. Special Number
3. Sorting Word
4. Fibonacci String
5. Array Sum
6. Evil Number

2 Practice Questions

1. Accept a string and replace


a few characters with other
strings.
2. Accept a sequence of
numbers and check if it is a
PseudoArithmetic sequence

3 Recursion

1. Recursion addition
2. Pseudo return
3. Recursion factorial
4. Number to word
5. Recursion palindrome 1
6. Recursion palindrome 2
7. Recursion remainder
8. Recursion power
9. Recursion GCD
10. Recursion binary search
11. Recursion magic number
12. Recursion decimal to
binary string
13. Recursion decimal to binary
14. Recursion guess number
15. Recursion power
16. Recursion palindrome String
17. Recursion armstrong
18. Recursion prime factorisation
19. Recursion octal to decimal

4 Inheritance
1. Question 2024 specimen
paper
2. Calculating area and volume
of a few shapes
Term 2 Paper Questions

Question 1) Armstrong

public class Armstrong


{
int num;
Armstrong(int
nx)
{
num = nx;
}
int cubeDigit(int q)
{
return (q*q*q);
}
int getDigitSum()
{
int sum =
0; int t =
num;
while (t!
=0)
{
int r = t%10;
int c =
cubeDigit(r);
sum += c;
t = t/10;
}
return sum;
}
boolean isArmstrong()
{
if(getDigitSum() ==
num) return true;
else
return false;
}
void display()
{
boolean b =
isArmstrong(); if (b ==
true)
System.out.println("armstrong
number"); else
System.out.println("not an armstrong number");
}
static void main()
{
Armstrong ob = new Armstrong(153);
ob.display();
}
}

Question 2)Special Number

public class Special


{
int N;
Special(int
num)
{
N = num;
}
void sum()
{
int l = N
%10; int t
= N;
int k = 0;
while(t!=0)
{
k++;
t = t/10;
}
int f = N/(int)Math.pow(10,3);
System.out.println("sum of first and last digit = " + (f+l));
}
void isSpecial()
{
int t = N;
int sum =
0;
while(t!
=0)
{
int f = 1;
int r = t%10;
for(int
i=1;i<=r;i++)
{
f = f*i;
}
sum
+= f; t
= t/10;
}
if (sum == N)
System.out.println("special
number"); else
System.out.println("not a special number");
}
static void main()
{
Special ob = new Special(145);
ob.sum();
ob.isSpecial();
}
}

Question 3)Sorting Word

import java.util.*;
public class
SortWord
{
String
text; int
len;
SortWord
()
{
text =
null; len
= 0;
}
void readText()
{
Scanner sc = new
Scanner(System.in);
System.out.println("enter a word");
text = sc.next();
}
void sortText()
{
char a[] = new
char[text.length()]; for(int
i=0;i<a.length;i++)
a[i] = text.charAt[i];
for(int i=0;i<a.length-1;i++)
{
for(int j=0;j<a.length-1-i;i++)
{
if(a[j]>a[j+1])
{
char t =
a[j]; a[j] =
a[j+1];
a[j+1] =
t;
}
}
}
System.out.print("sorted
word = "); for(int
i=0;i<a.length;i++)
System.out.print(a[i]);
}
static void main()
{
SortWord ob = new SortWord();
ob.readText();
ob.sortText();
}
}

Question 4)Fibonacci String

import java.util.*;
public class
FiboString
{
String
x;
String
y;
String
z; int
n;
FiboString()
{
x = "a";
y = "b";
z = "ba";
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the number of
terms"); n = sc.nextInt();
}
void generate()
{
System.out.print(x + " ");
System.out.print(y +
" "); for(int
i=2;i<=n;i++)
{
z=y+
x; x =
y;
y = z;
System.out.print(z +
" ");
}
}
static void main()
{
FiboString ob = new FiboString();
ob.accept();
ob.generate();
}
}

Question 5)Array sum

import java.util.*;
public class
ArraySum
{
int s;
int m[][];
int n[][];
int k[][];
ArraySum(int
x)
{
s = x;
m = new int[s][s];
n = new int[s][s];
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter " + s*s + "
numbers"); for(int i=0;i<s;i++)
for(int j=0;j<s;j+
+) m[i][j] =
sc.next();

System.out.println("enter " + s*s + "


numbers"); for(int i=0;i<s;i++)
for(int
j=0;j<s;i++)
n[i][j] =
sc.next();
}
void calcSum()
{
for(int i=0;i<s;i++)
for(int j=0;j<s;j+
+) k[i][j] = m[i][j]
+ n[i][j];
}
void display()
{
for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
System.out.print(m[i]
[j]);
System.out.println();
}

for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
System.out.print(n[i]
[j]);
System.out.println();
}

for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
System.out.print(k[i]
[j]);
System.out.println();
}
}
}

Question 6)Evil Number

import java.util.*;
public class Evil
{
int nnum;
int bin;
Evil()
{
nnum = 0;
bin = 0;
}
void acceptNum()
{
Scanner sc = new
Scanner(System.in);
System.out.println("enter a
number"); nnum = sc.nextInt();
}
void convertbin(int x)
{
int c = 1;
while(x!=0)
{
int r = x
%2; x =
x/2; bin
+= r*c; c
= c*10;
}
}
void check()
{
convertbin(nnum
); int k = 0;
while(bin!=0)
{
int r = bin
%10; bin =
bin/10;
if(r==1)
k++;
}
if(k%2 == 0)
System.out.println(nnum + " is an evil
number"); else
System.out.println(nnum + " is not an evil number");
}
}

Practice Questions
Question 7)String Replace

import java.util.*;
public class StringReplace
{
String
n; int
s;
int
p;
int
A;
int
B;
int
C;
int
D;
int
E;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter a 3 letter word and two integers greater than 1 or
10" less than
);
while(true)
{
n = sc.next(); n =
n.toUpperCase(); s =
sc.nextInt();
p = sc.nextInt();
if(n.length() == 3 && s>=1 && s<=10 && p>=1 && p<=10)
{
int k = 0;
for(int i=0;i<n.length();i++)
{
char c = n.charAt(i);
if(c == 'A' || c == 'B' || c == 'C' || c == 'D' ||
c == 'E') k++;
}
if(k ==
3)
break;
else
System.out.println("invalid input.enter again");
}
else
System.out.println("invalid input. enter again");
}
}
void change()
{
String str = "";
for(int
i=0;i<s;i++)
{
if(p>=n.length())
{
for(int j=0;j<n.length();j++)
{
if(n.charAt(j) ==
'A') str += 'B';
else if(n.charAt(j)
== 'B') str +=
"AB";
else if(n.charAt(j)
== 'C') str +=
"CD";
else if(n.charAt(j)
== 'D') str +=
"DC";
else
str += "EE";
}
}
else
{
for(int j=0;j<p;j++)
{
if(n.charAt(j) ==
'A') str += 'B';
else if(n.charAt(j)
== 'B') str +=
"AB";
else if(n.charAt(j)
== 'C') str +=
"CD";
else if(n.charAt(j)
== 'D') str +=
"DC";
else
str += "EE";
}
}
n=
str;
str =
"";
}

for(int i=0;i<n.length();i++)
{
char c =
n.charAt(i); if(c
== 'A')
A++;
else if(c ==
'B') B++;
else if(c ==
'C') C++;
else if(c ==
'D') D++;
else
E+
+;
}
}
void display()
{
System.out.println("new string = "
+ n); System.out.println("A\tB\tC\
tD\tE");
System.out.println(A + "\t" + B + "\t" + C + "\t" + D + "\t" + E);
}
static void main()
{
StringReplace ob = new
StringReplace(); Scanner sc = new
Scanner(System.in); while(true)
{
ob.input();
ob.change();
ob.display();
System.out.println("do you wish to continue? enter 1
for yes"); int a = sc.nextInt();
if(a !=
1)
break;
}
}
}

Question 8)Pseudo Arithmetic

import java.io.*;

class Pseudoarithmetic
{
public int n;
public int
a[]; public
int ans;
public int
flag; public
int sum;
public int r;

Pseudoarithmetic()
{
n = 0;
flag = 0;
sum = 0;
}

void accept(int nn)


{
n = nn;
a = new int[n];
BufferedReader B = new BufferedReader(new
InputStreamReader(System.in));

for (int i = 0; i < n; i++) {


{
a[i] =
Integer.parseInt(B.readLine());
sum = sum + a[i];
}

}
boolean check()
{
if (n % 2 == 0)
{
int i = 0, p = n -
1; while (i < p)
{
r = a[i] + a[p];
if (r == (a[i + 1] + a[p - 1]) && (r * 3) ==
sum) { flag = 0;
}
els
e
{ flag = 1;
return
false;
}

p=p-
1; i = i
} + 1;
}
els
e
{

int i = 0, p = n -
1; while (i <= p)
{
r = a[i] + a[p];
if (r == (a[i + 1] + a[p - 1]) && (r * 3) ==
sum) { flag = 0;
}
els
e
{ flag = 1;
return
false;
}

p=p-
1; i = i
+ 1;
}
}
if (flag ==
0) return
true;
else
return false;
}
}
public static void main()
{
Pseudoarithmetic obj = new Pseudoarithmetic();
obj.accept(6);
boolean isPseudoArithmetic =
obj.check(); if (isPseudoArithmetic)
{
System.out.println("The sequence is a pseudo arithmetic sequence.");
}
else
{
System.out.println("The sequence is not a pseudo arithmetic sequence.");
}
}

Recursion
public class infinite// when a method calls itself it is called as recursive method
{
static void show()//Infinite recursion happens when base case is missing
{
System.out.println("Hello");
show(); // recursive case
}

Question 9)Recursion addition

class RecurAdd
{
static void main()
{
int a;
Scanner sc=new
Scanner(System.in);
System.out.print("Enter any
number->"); a=sc.nextInt();
System.out.println("Summation of 1 to "+a+" = "+Add(a));
}
static int Add(int n)
{
if (n<2) //base
case return 1;
return n+ Add(n-1); //recursive case
}
/* Output tracing for static int Add(5)
* return 5 + Add(4)
* return 5 + return 4 + Add(3)
* return 5 + return 4 + return 3 + Add(2)
* return 5 + return 4 + return 3 + return 2 + Add(1)
* return 5 + return 4 + return 3 + return 2 + return 1
* return 5 + return 4 + return 3 + return 2 + 1
* return 5 + return 4 + return 3 + 3
* return 5 + return 4 + 6
* return 5 + 10
* 15
*/
}

Question 10)Pseudo return

public class PseudoReturn


{
public static void sampleMethod()
{
System.out.println("Hell
o"); return ;// it returns
nothing
}
}
public class RecurDisplay
{
static void main()
{
int a;
Scanner sc= new
Scanner(System.in);
System.out.print("Enter any
number->"); a=sc.nextInt();
show1(a);
System.out.println("do you like recursion?");
}

static void show(int n)


{
if(n==0)
return; //base case with pseudo return statement
//provides the escape route for ending the recursive case.
System.out.println(n);
show(n-1);// Recursive case

static void show1(int n)


{
if(n>0) //base case
{
System.out.println(n);
show1(n-1);//recursive
case
}
}
}

Question 11)Recursion factorial

import java.io.*;
class RecurFact
{
static void main()throws IOException
{
int a;
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); System.out.print("Enter any number->");
a=Integer.parseInt(in.readLine());
System.out.println("Factorial of "+a+" =
"+factorial1(a));
}
static int factorial(int n)
{
if (n<2)
return 1; //base case
return n*factorial(n-1); //recursive case
}
static int factorial1(int n)
{
return ((n<2)?1:n*factorial1(n-1));
}
}

Question 12)Number to word

public class NumWord


{
int n;
String
s="";
NumWord(int x)
{
n=x;
}
void extd(int no)
{
if(no==
0)
retur
n;
else
{
int dig=no
%10;
show(dig);
extd(no/10);
}
}

void show(int d)
{
String
name[]={"zero","one","two","three","four","five","six","seven","eight",
"nine"}; for(int i=0;i<=9;i++)
if(d==i)
s=name[i]+"
"+s;
}
void display()
{
extd(n);
System.out.println(s);
}
public static void main()
{
NumWord o = new
NumWord(256); o.display();
}
}

Question 13)Recursion palindrome

public class Palin_recur


{
String w;
void
input()
{
Scanner sc= new
Scanner(System.in);
System.out.println("Enter your
word"); w = sc.next();
System.out.println(checkPalin(0))
;
}

boolean checkPalin(int n)
{
int l =
w.length();
if(n < l)
{
char c = w.charAt(n);
char c1 = w.charAt(l-
n-1);

if(c==c1)
{
checkPalin(n+1);
return(true);
}
else
return(false);
}
else
return(false);
}

public static void main()


{
Palin_recur ob = new Palin_recur();
ob.input();
}
}

Question 14)Palindrome

import java.util.*;
public class Palindrome
{
int no,n;
String
str,p="";

void input()
{
Scanner sc = new Scanner
(System.in);
System.out.print("Enter the
number: "); no=sc.nextInt();

System.out.print("Enter the
String: "); str=sc.next();
}

String ispalin(String str)


{
int
l=str.length();
if(l==0)// base
case
return p;
else
{
p=p+str.charAt(l
-1); l=l-1;
return(ispalin(str.substring(0,l)));// recursive case
}
}

int ispalin(int no)


{
int temp=0;

if(no==0)// base
case return n;
else
{
temp=no%10;
n=n*10+temp
;
return(ispalin(no/10));//recursive case
}
}

void check()
{
System.out.print('\u000c');

if(str.equalsIgnoreCase(ispalin(str)))
{
System.out.println("The word "+str+" is a Palindrome Word");
}
else
{
System.out.println("The word "+str+" is not a Palindrome Word");
}

if(no==ispalin(no))
{
System.out.println("The number "+no+" is Palindrome Number");
}
else
{
System.out.println("The number "+no+" is not Palindrome Number");
}
}

public static void main()


{
Palindrome ob=new Palindrome();

ob.input();
ob.check();
}
}

Question 15)Recursion remainder

public class Remainder


{
int n;
int
div;

void input()
{
Scanner sc = new Scanner
(System.in);
System.out.print("Enter the
number"); n=sc.nextInt();

System.out.print("Enter the
divisor"); div=sc.nextInt();
}
int remain(int n)
{
if(div>n)
return n;
return remain(n-div);
}

public static void main()


{
Remainder ob=new Remainder();
ob.input();
System.out.println("The remainder is "+ob.remain(ob.n));
}
}

class RecurRemainder
{
static int guessWhat (int p, int q)
{
if (p>=q)
{
p=p-q;
return guessWhat (p, q);
}
else
return p;
}

static void findRem(int a, int b)


{
if(a-b<0)
{
System.out.println(a);
}
else
{
a=a-b;
findRem(a,b);
}
}

static void main()


{
System.out.println(guessWhat(205,10));
}

static int task(int m, int n)


{
if(m==n)
return
m; else
if(m>n)
return task(m-
n,n); else
return task(m,n-m);
}

Question 16)Recursion power

import
java.util.*;
class
RecurPOW
{
static long power(int x,int y)
{
if (y==0)
return 1; //base case
return x*power(x,y-1); //recursive case
}
static void main()
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any number-
>"); a=sc.nextInt();
System.out.print("Its Exponent->");
b=sc.nextInt();
System.out.println(a+"^"+b+" = "+power(a,b));
}

import java.util.*;
class RecurFibo
{
static void fib(int x,int a,int b)
{
int c=a+b;
System.out.print(c+"
\t"); a=b;
b=c;
if(x<1)
return;//escape route- psuedo
return fib(x-1,a,b);
}
static void main()
{
int
a,b,l;
a=0;
b=1;
Scanner sc=new
Scanner(System.in);
System.out.println("How many
terms?"); l=sc.nextInt();
System.out.print(a+"\t"+b+"\t");
fib(l-3,0,1);
}
}

Question 17)Recursion GCD


class RecurGCD
{
static int gcd1(int a,int b)
{
if(a==b)
return(a);
else
if(a>b)
{
a=a-b;
return gcd1(a,b);
}
else if(b>a)
{
b=b-a;
return gcd1(b,a);
}
return 0;
}
static int gcd2(int a,int b)
{
if(a
%b==0)
return b;
else
return(gcd2(b,a%b));
}
void main()
{
int
a=6;
int
b=4;
System.out.println(gcd1(a,b));
}
}

Question 18)Recursion binary search

import java.io.*;
public class RecurBinarySearch
{
int arr[],l;
static BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); RecurBinarySearch(int l)
{
this.l=l;
arr=new
int[l];
}

void input()throws IOException


{
for(int i=0;i<l;i++)
{
System.out.print("Enter number "+ (i+1)+"->");
arr[i]=Integer.parseInt(in.readLine());
}
}

void sort()
{
int i,j,min,pos,t;
for(i=0;i<arr.length-
1;i++)
{
min=arr[i];
pos=i;
for(j=i+1;j<arr.length;j++)
{
if(arr[j]<min)
{
min=arr[j]
; pos=j;
}
}
t=arr[i];
arr[i]=arr[pos];
arr[pos]=t;
}
}

public int getIt(int x,int f,int l)


{
if(f>l)
return -
1; int
m=(f+l)/2;
if(arr[m]<x
)
return
getIt(x,m+1,l); else
if(arr[m]>x)
return
getIt(x,f,m-1); else
return m;
}

public static void main()throws IOException


{
int
size,no,flag;
String res;
System.out.print("Enter the array size :");
size=Integer.parseInt(in.readLine());
RecurBinarySearch ob = new
RecurBinarySearch(size); ob.input();
ob.sort();
while(true)
{
System.out.print("Enter the number to be searched :");
no=Integer.parseInt(in.readLine());
flag
=ob.getIt(no,0,ob.l-
1); if(flag==-1)
System.out.println("Number not
found"); else
System.out.println("Number found at position: "+(flag+1));
System.out.print("Continue search?");
res=in.readLine();
if
(res.equalsIgnoreCase("no"
)) break;
}
}
}

Question 19)Recursion magic number

import java.io.*;
public class
Magic
{
int s,n;
InputStreamReader in=new
InputStreamReader(System.in); BufferedReader
bf=new BufferedReader(in);
void input()throws IOException
{
System.out.print("Enter a no :");
n=Integer.parseInt(bf.readLine());
}
int isSumOneDigit(int no)
{s
=0;
while(no>0)
{
s+=no%10;
no/=10;
}
if(s<10
)
return
s; else
return (isSumOneDigit(s));
}
boolean isMagic(int no)
{
if(no==1)
return
true; else
return false;
}
public static void main()throws IOException
{
Magic m=new
Magic(); m.input();
if(m.isMagic(m.isSumOneDigit(m.n)))
System.out.println(m.n+ " is a magic
number"); else
System.out.println(m.n+ " is not a magic number");
}

}
Question 20)Recursion decimal to binary string

import java.io.*;
public class DectoBinStr
{
int n;
String
s="";

void getData() throws IOException


{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); System.out.print("Enter the decimal
number: "); n=Integer.parseInt(in.readLine());

void recursive(int temp)


{
if(temp==0)
{
return;
}
else
{
int y=temp
%2; s=y+s;
temp=temp/2;
recursive(tem
p);
}

void putData()
{
System.out.println("the binary equivalent is " + s);
}

public static void main() throws IOException


{
DectoBinStr db= new
DectoBinStr(); db.getData();
db.recursive(db.
n); db.putData();

}
}

Question 21)Recursion decimal to binary

import java.io.*;
public class
DectoBin
{

int
s;
int
n;

DectoBin()
{
n=0;
s=0;
}
void getData() throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number");
n=Integer.parseInt(in.readLine());

int recursive(int temp)


{
if(temp==0)
{
return 0;
}
else
{
int y=temp
%2;
s=s*10+y;
temp=temp/2;
return recursive(temp);
}

void putData()
{
int t=n;
recursive(t);
System.out.println("the binary equivalent of " + n +" is " + s);
}

public static void main() throws IOException


{
DectoBin db= new DectoBin();
db.getData();
db.putData();
}
}
Question 22)Recursion guess number

class Recurguess
{
static void main()
{
int
a=4;
int
b=3;
System.out.println(guessWhat(a,b));
}
static int guessWhat (int p, int q)
{
if (q==0)
return 0;
else if
(q==1)
return p;
else if
(q>0)
return p + guessWhat (p,q-
1); else
{
q=(int)Math.abs(q);
return (-1*(p + guessWhat(p,q-1)));
}
}
}

Question 23)Recursion power

import java.io.*;
public class Power_Recursion
{
int a,b, n;
Power_Recursion()
{
n=1;
}
public void main()throws IOException
{
//int a,b;
Power_Recursion p=new Power_Recursion();
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); System.out.print("Enter the base->");
a=Integer.parseInt(in.readLine());
System.out.print("Enter the power-
>");
b=Integer.parseInt(in.readLine());
System.out.println("a^b="+p.power
(a));
}
public long power(int a)
{

if(n>b)
return 1;
else
{
n=n+1;
return (a*power(a));
}
}
}

Question 24)Recursion palindrome string

public class QuestionRec


{
static void spell_num(int number)
{
if(number < 10)
{
System.out.println(number);
}
else
{
spell_num((int) Math.floor(number
/ 10)); System.out.println(number
% 10);
}
}

static int Fib(int N)


{
if (N == 0 || N == 1)
return N;

return (Fib(N-1) + Fib(N - 2));


}

static void printFun(int test)


{
if (test <
1)
return;

else {
System.out.print("

"+test); printFun(test -

2); System.out.print("

"+ test);

}
}

static String reverse_sentence(String str)


{
// check if str is
empty if
(str.isEmpty())
// return the
string return
str;
else {

// extract the character at 0th index, that is


// the character at
beginning char ch =
str.charAt(0);

// append character extracted at the end


// and pass the remaining string to the
function return
reverse_sentence(str.substring(1)) + ch;
}
}

static boolean palindrome(int arr[], int begin, int end)


{
// base case
if (begin >=
end) { return
true;
}
if (arr[begin] == arr[end]) {
return palindrome(arr, begin + 1, end - 1);
}
else {
return false ;
}
}

public static void main()


{
//spell_num(321);
// System.out.println(Fib(9));
//printFun(10);
System.out.println(reverse_sentence("sang is a good girl."));
}
}

Question 25)Recursion armstrong

public class Armstrong


{
private int n;
Armstrong(int n)
{
this.n=n;
}

public boolean isArmstrong(int n)


{

if( n==sumOfPowers
(n)) return true;
else
return false;
}

public int sumOfPowers(int num)


{
if (num == 0)
{
return 0;
}
int digit = num % 10;
return (int) Math.pow(digit, 3) + sumOfPowers(num/10);
}

public void show()


{
if(isArmstrong(n))
System.out.println(n+" is an Armstrong
number"); else
System.out.println(n+" is an Armstrong number");
}

public static void main()


{
int n=153;
Armstrong arm=new Armstrong(n);
arm.show();
}
}

Question 26)Recursion prime factorisation

public class PrimeFactorisation


{
public static void puzzle(int p, int q)
{
if(p>1)
{
if(p%q!=0)
puzzle(p,q+
1);
else
{
System.out.println(q+" ");
puzzle(p/q,q);
}
}
}
}

Question 27)Recursion octal to decimal

import java.io.*;
public class
OctDeci
{

int octal;
int deci;

OctDeci()
{
octal=0;
deci=0;
}
void getNum() throws IOException
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); System.out.println("Enter the number");
octal=Integer.parseInt(in.readLine());

int change(int oct,int i)


{
if(oct==
0)
return
0;
int digit=oct%10;
return (int)Math.pow(8,i)*digit+change(oct/10,++i);
}
public void show()
{
deci=change(octal,0);
System.out.println("Octal number:
"+octal);
System.out.println("Decimal Equivalent: "+deci);
}

public static void main()throws IOException


{
OctDeci od=new
OctDeci(); od.getNum();
od.show();
}

Inheritance

Question 28)Question 2024 specimen paper

import java.util.*; //super class


public class Circle
{
protected double
radius; protected
double area;
Circle( double radius)
{
this.radius=radius ;
}
void cal_area()
{
area=3.14*radius*radius;
}
void display()
{
System.out.println("area="+area);
}
}

import java.util.*; // subclass


public class Volume extends Circle
{
private double
height; private
double volume;
Volume( double radius, double height)
{
super(radius);
this.height=heig
ht;
}
double Calculate()
{
volume=area*height
; return volume;
}
void display()
{
System.out.println("volume="+Calculate
()); super.display();
}
public static void main ()
{
Volume v = new
Volume(); v.display();
}
}

Question 29)Calculating area and volume of a few shapes


//Abstract class- super class of Cube, Cylinder and Cone
abstract class AreaCalc
{
abstract double area(double x, double y);
abstract double volume(double a, double b, double c);
}

//Subclass of AreaCalc, calculates area of cube


public class Cube extends AreaCalc
{
double area(double l, double b)
{
return 6*l*b;
}
double volume(double l, double b, double h)
{
return l*b*h;
}
}

//Subclass of AreaCalc, calculates area of Cone


public class Cone extends AreaCalc
{
double area(double r, double l)
{
return 3.14*r*l;
}
double volume(double r, double h, double l)
{
return (1/3)*3.14*r*r*h;
}
}

//Subclass of AreaCalc, calculates area of Cylinder


public class Cylinder extends AreaCalc
{
double area(double r, double h)
{
return (2*3.14*r*h)+(2*3.14*r*r);
}
double volume(double r, double h, double l)
{
return 3.14*r*r*h;
}
}

You might also like