Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

-Computer Project

Uploaded by

sahilpalit2478
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

-Computer Project

Uploaded by

sahilpalit2478
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

COMPUTER SCIENCE

PROJECT
NAME:SAHIL PALIT
CLASS:XI
SECTION:B
ROLL NO:20
SESSION:2022-23
QUESTION NO. 1
Design a class BinToDec to convert a binary number to its equivalent decimal number.
Some
of the members of the class are given below:
Class name : BinToDec
Data members/instance variables
n : an integer to store the binary number
Member functions:
BinToDec( ) : non-parameterized constructor
void accept( ) : to accept a binary number
int convert(int) : to convert a binary number to its equivalent
decimal number and return it
void display( ) : to convert the binary number to decimal
number by calling the function int convert(int)
and display the result.
ALGORITHM
STEP 1: Start

STEP 2:Declaring class BinToDec

STEP 3:Declaring integer variable n to store the number inputted by the user

STEP 4:Creating object ob when non-parameterised constructor gets invoked

STEP 5:Definition of non-parameterized constructor

n 0

End of non-parameterised constructor

STEP 6:Calling void accept( ) using object ob

Definition of void accept( )

Creating object sc of Scanner class

Printing ENTER THE BINARY NUMBER

n sc.nextInt()

End of void accept( )

STEP 7:Calling void display( ) using object ob

Definition of void display( )

Declaring integer variable decimal to store the value returned by int convert(int b)

Calling int convert(int b)

Definition of int convert(int b)

Declaring integer variable d,deci and p and initializing deci, p with 0

while b!=0

d b%10

deci deci+(d*(int)(Math.pow(2,p)))

p++

b b/10

End of while

Return deci

End of int convert(int b)

decimal convert(n)

Printing The decimal number is

Printing decimal

End of void display( )

STEP 9:End of main( )

STEP 10:End of class BinToDec


STEP 11:End
PROGRAM
import java.util.*;

public class BinToDec{

int n;

BinToDec(){

//initializing n

n=0;

void accept(){

Scanner sc=new Scanner(System.in);

//accepting the binary number from the user

System.out.println("ENTER THE BINARY NUMBER");

n=sc.nextInt();

int convert(int b){

int p=0;

int d;

int deci=0;

//converting to decimal

while(b!=0){

d=b%10;//extracting last digit

//adding to deci the last digit multiplied by 2 raised to the power of the position of the digit taken from right to left

deci=deci+(int)(d*(Math.pow(2,p)));

b=b/10;

p++;//incrementing position

return deci;

void display(){
int decimal=convert(n);

//display binary number converted to decimal

System.out.print("The decimal number is ");

System.out.print(decimal);

public static void main(String[]args){

BinToDec ob=new BinToDec();//creating object ob of class BinToDec

//calling members functions using object ob

ob.accept();

ob.display();

}
SAMPLE INPUT AND OUTPUT
VARIABLE TABLE

NAME TYPE DESCRIPTION


n int Stores the binary number
input by the user
b int Stores the binary number
input by the user
d int Temporarily stores each
digit of the binary number
deci Int Stores the binary number
converted to decimal
p int Stores position of each digit
of binary number
decimal int Stores decimal number
returned by int convert(int
b)
sc Scanner Helps to accept input from
the user
ob BinToDec Helps to call the member
functions of class BinToDec
QUESTION NO. 2
The potential of a word is found by adding the ASCII values of the letters. (ASCII values of
A
to Z are 65 to 90).

Example: BALL
Potential = 66 + 65 + 76 + 76 = 283
Write a program to accept a sentence which may be terminated by either “ . ” , “ ? ” or “ ! ”
only. The words of sentence are separated by single blank space and are in UPPER CASE.
Decode the words according to their potential and arrange them in ascending order of their
potential strength. Test your program with the following data and some random data:

Example 1:
INPUT: HOW DO YOU DO?
OUTPUT: HOW = 238
DO = 147
YOU = 253
DO = 147
DO DO HOW YOU

Example 2:
INPUT: LOOK BEFORE YOU LEAP.
OUTPUT: LOOK = 309
BEFORE = 435
YOU = 253
LEAP = 290
YOU LEAP LOOK BEFORE

Example 3:
INPUT: HOW ARE YOU#
OUTPUT: INVALID INPUT
ALGORITHM
STEP 1:Start

STEP 2:Declaring class Potential

STEP 3:Declaring string variable s to store the sentence and integer variable l to store the length of the string

STEP 4:Creating main() for the creation of object of the class Potential to call the member functions of the
class

STEP 5:Creating object ob when the non-parameterised constructor gets invoked

STEP 6:Definition of non-parameterised constructor

s “”

l 0

End of non-parameterised constructor

STEP 7:Calling void sentencePotential_Sort() using object ob

Definition of void sentencePotential_Sort()

Creating object sc of Scanner class to accept input from user

Printing ENTER A SENTENCE ENDING IN .,!?

s sc.nextLine()

l s.length()

Creating object str of class StringTokenizer to divide sentence into tokens

if s.charAt(l - 1) == '.'||s.charAt(l - 1) == ','||s.charAt(l - 1)== '!'||s.charAt(l - 1)== '?'

wn str.countTokens()

String s1[ ] new String[wn]

Declaring integer variables k,I and initializing k with 0

for i 0 to i<wn step value 1

s1[k++] str.nextToken()

Declaring integer variables j,temp,p1 and initializing temp,p1 with 0

Declaring String variable t1 and inititalizing t1 with “”

int P[ ] new int[wn];

for i 0 to i<wn step value 1

for j 0 to j<s1[i].length() step value 1

p1 p1+ (int)(s1.charAt(j))

End of for

P[i] p1

p1 0

End of for
for i 0 to i<s1.length – 1 step value 1

for j 0 to j<P.length - i -1 step value 1

if P[j]>P[j+1]

temp P[j]

P[j] P[j+1]

P[j+1] temp

t1 s1[j]

s1[j] s1[j+1]

s1[j+1] t1

End of if

End of for

End of for

for i 0 to i<wn step value 1

Printing s[i]

End of for

End of if

Else

Printing INVALID INPUT

End of else

End of void sentencePotential_Sort()

STEP 8:End of main()

STEP 9:End of class Potential

STEP 10:End
PROGRAM
import java.util.*;

public class Potential {

String s;

int l;

Potential()

s="";

l=0;

void sentencePotential_Sort()

Scanner sc = new Scanner(System.in);

//accepting a sentence from the user

System.out.println("ENTER A SENTENCE ENDING IN .,!?");

s = sc.nextLine().toUpperCase();

l = s.length();//finding the length of the string

StringTokenizer str = new StringTokenizer(s, " .,!?");

//checking if the word ends with '.';',';'!' or '?'

if (s.charAt(l - 1) == '.'||s.charAt(l - 1) == ','||s.charAt(l - 1)== '!'||s.charAt(l - 1)== '?') {

int wn = str.countTokens();//counting number of words in the sentence

String s1[] = new String[wn];

int k = 0;

int i;

for (i = 0; i < wn; i++) {

s1[k++] = str.nextToken();//adding words of sentence to array

int j;

int temp = 0;

String t1="";

int p1 = 0;

int P[] = new int[wn];


for (i = 0; i < wn; i++) {

for (j = 0; j < s1[i].length(); j++) {

p1 = p1 + (int) (s1[i].charAt(j));//adding ASCII value of each letter in a word

P[i] = p1;

p1 = 0;

for (i = 0; i < wn; i++) {

System.out.println(s1[i] + " = " + P[i]);//displaying words with potential

//arranging sentence in ascending order of their potential strength using bubble sort

for (i = 0; i < s1.length - 1; i++) {

for (j = 0; j < P.length - i - 1; j++) {

if (P[j] > P[j + 1]) {

temp= P[j];

P[j] = P[j + 1];

P[j + 1] = temp;

t1 = s1[j];

s1[j] = s1[j + 1];

s1[j + 1] = t1;

for (i = 0; i < wn; i++) {

System.out.print(s1[i] + " ");//displaying sorted sentence

else {

System.out.println("INVALID INPUT");

public static void main(String []args)

//creating object of class Potential


Potential ob=new Potential();

//calling members functions using object ob

ob.sentencePotential_Sort();

}
SAMPLE INPUT/OUTPUT
VARIABLE TABLE
NAME TYPE DESCRIPTION
sc Scanner Helps to accept input
form user
str StringTokenizer Helps to break string into
tokens
s String Stores sentence input by
the user
l int Stores length of
sentence input by user
wn int Stores number of words
in the sentence
s1 String array Stores words of the
sentence
k int Array index for each
iteration
t int Stores temporary value
of elements of P[]

t1 String Temporarily stores


elements of s1[] during
swapping
pt int Temporarily stores
potential of each word of
the sentence
P int array Stores potentials of the
words of the sentence
i int Loop variable
j int Loop variable
ob Potential Helps to call the member
functions of class
Potential

You might also like