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

di

The document is a project report by Divyansh Nigam from St. Lawrence School, detailing various programming tasks and solutions in Java. It includes a table of contents, acknowledgments, and multiple coding questions covering topics like number classification, string manipulation, and sorting algorithms. Each question is accompanied by a solution and variable descriptions, showcasing the author's learning experience in computer science.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

di

The document is a project report by Divyansh Nigam from St. Lawrence School, detailing various programming tasks and solutions in Java. It includes a table of contents, acknowledgments, and multiple coding questions covering topics like number classification, string manipulation, and sorting algorithms. Each question is accompanied by a solution and variable descriptions, showcasing the author's learning experience in computer science.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

1

Title /Cover Page

Divyansh Nigam
15, October,2024
St. Lawrence School
10- ‘C’
2

Tabel of Content
Page no Ques ons
1) Title page
2) Tabel of Content
3) Acknowledgement
4) Q1 Superspy number
5) Q2 Duck number
6) Q3 (i)Binary number to Decimal
7) (ii)Decimal number to Binary
8) Q4 Special Armstrong number
9) Q5 Overload func on sumseries(int n , double x)(1)
10) (2) sumseries(int start, int end)
11) Q6 Overload func on joystring(String s)(1)
12) (2)void joystring(String str, int p)
13) Q7 (i)pa ern 1
14) (ii)pa ern 2
15) Q8 Maximum number of characters
16) Q9 Convert to uppercase
17) Q10 To print the frequency of the given word in the sentence
18) Q11 To Display the words which begin and ends with a same character
19) Q12 To display the names in ascending order using the Bubble sort technique
20) Q13 To display obtained in descending order using Selec on sort technique
21) Q14 To search for an integer value input by the user using Binary search technique
22) Q15 To display the sum of each row in 4x4 array
23) Q16 To accept the names of 10 students in an array and check for the existence of the
given name in the array using “Linear search technique”
24) Q17 Special array for 3x3 array
25) Q18 To store numbers in 4×4 array in double dimensional array and display the largest
and the smallest value within the array
26) Q19 class Stock
27) …………..
28) Q20 class SmartShop
29) ………….
30) References
3

ACKNOWLEDGEMENT
I would like to take this opportunity to express my hear elt apprecia on to
everyone who contributed to the successful comple on of my computer project. This
project has been a significant learning experience for me, and I am grateful for the
support and guidance I have received along the way.
First and foremost, I want to extend my sincere thanks to my Computer
Teacher, Mrs. Farheen Fa ma Your exper se, encouragement, and willingness to
answer my ques ons have been invaluable throughout this project. Your dedica on
to teaching and your passion for the subject have inspired me to delve deeper into
the world of computer science.
I would also like to acknowledge the role of our school principal, Fr. Olwin
J.Moras. Your leadership and vision have created an educa onal environment that
encourages students to pursue their interests and excel in their chosen fields. Your
support for our endeavors has been a driving force behind our success.
While this project was an individual effort, the collec ve support of my
teacher and principal, along with the encouragement from my loved ones, has made
it possible. I am grateful to each and every one of you for being a part of my journey
and for helping me achieve this milestone.
Thank You.
Divyansh Nigam
4

Ques on 1: Define a class to accept a number and check whether it is a SUPERSPY number
or not. (A number is called SUPRSPY if the sum of the digits equals the number of the
digits.)
Solu on: -
import java.u l.Scanner;
public class $1
{
public sta c void main(String args[])
{ int s=0;int x=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");==
int n=sc.nextInt();
int d=n;
while(n>0)
{int l=n%10;
s=s+l;
n=n/10;
x++;
}
if(x==s)
{
System.out.println("SUPERSPY number");
}
else
{ System.out.println(": Not an SUPERSPY number"); } } }
Variable descrip on: -

int n To store the number input by the user


int d To copy the number input
int l To find the last digit of the number
int x To count the number of digit
5

int s To sum the digits

Ques on 2: Define a class to accept a 3-digit number and check whether it is a duck number
or not.
Solu on: -
import java.u l.Scanner;
public class $2 {
public sta c void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();int s=0;
if((n>99)&&(n<1000))
{ while(n>0){
int l=n%10;
if(l==0)
{ s=1;break;}
else
{ s=0; }
n=n/10;
}
if(s==1)
{ System.out.println("Duck number"); }
else
{ System.out.println("Invalid"); }
else
{ System.out.println("Invalid"); }
}
}
Variable descrip on: -

int n To store the number input by the user


6

int s To check if the number is Duck number or not


int l To check if the digits equals to zero

Ques on 3: Write a menu driven class (using switch case) to perform the following:
(i) Input a Binary number (base 2) and convert it into its decimal equivalent (base 10)
(ii) Input a Decimal number (base 10) and convert it into its Binary equivalent (base 2)
Solu on: -
import java.u l.Scanner;
public class $3
{
public sta c void main(String args[])
{ Scanner sc= new Scanner(System.in);
System.out.println("Enter 1 for binary to decimel conversion");
System.out.println("Enter 2 for decimel to binary conversion");
int x=sc.nextInt();
switch(x)
{
case 1:
System.out.println("Enter the number");

int n=sc.nextInt();
int i=0;
int s=0;
while(n>0)
{
x=n%10;
s=s+x*(int)Math.pow(2,i);
i++;
n=n/10;
}

System.out.println("The converted number is "+s);


7

break;

//(ii) Input a Decimal number (base 10) and convert it into its Binary equivalent (base 2)

case 2:
System.out.println("Enter the number");
int q=sc.nextInt();
int p;
String l=" ";
while(q>0)
{
p=q%2;
l=String.valueOf(p)+l;
q=q/2;
}
System.out.println("The converted number is "+l);
break;
}

}
}
Variable descrip on: -

int x To know the choice of conversion


int n To input the binary number
int i To find the digit of binary number
int s To evaluate the decimal number from the binary number
int q To input the decimal number
int p To find the remainder
String l To evaluate the binary number from the decimal number
8
9

Ques on 4: A number is said to be Special Armstrong if sum of its digits raised to the power
of length of the number is equal to the number. Write a program to generate and print all
the Special Armstrong numbers between 500 and 2000.
Solu on: -

public class $4 {
public sta c void main(String args[])
{
for(int i =500 ; i<=2000; ++i )
{
int n =i, num=i , sum=0 ; int count = 0 ; int rem = 0;
while (n >0)
{
n = n / 10;
count ++;
}
while (num>0)
{ rem = num %10;
sum = sum + (int)Math.pow(rem , count );
num = num/10;
}
if (i == sum ) { System.out.println(i);
}

}
}
Variable descrip on: -

int i To select the number


int n To store the number
int num To store the number
int sum To sum the digits raised to the power of the number of digits
int count To count the digits
int rem To find the last digit
10

Ques on 5:Design a class to overload a func on sumseries() as follows:


(1) void sumseries(int n , double x) - with one integer argument and one double argument to
find the display the sum of the series given below: 𝑆𝑢𝑚 =( 1/x^2)-(4/x^5)+(7/x^8)-
(10/x^9)+……..n/x^n
(2) void sumseries(int start, int end)- to calculate the sum of all prime numbers in the range
between start and end
Solu on:-
public class $5{
public void sumseries(int n,double x){
int l=2;
int s=-1;
int r=1;

double su=1/Math.pow(x,l);

for(int k=1;k<n;k++)
{
su=su+(r+3)*(1.0*s)/Math.pow(x,(l+3));

s=s*-1;
r=r+3;
l=l+3;
}
System.out.println("Sum "+su);
}
11

//(2)
public void sumseries(int start,int end)

{ int sum = 0;
for (int i = start; i <= end; i++)
{
int count = 0;
if (i > 1) { for (int j = 2; j <= Math.sqrt(i); j++)
{
if (i % j == 0) {count++;break; }
}
if (count == 0) { sum += i;
}
} } System.out.println("The sum of all prime numbers between " + start+ " and " + end + "
is: " + sum);
}
}
Variable descrip on: -

int n To input the limit of the series


int l To print the power at which x will be raised in the
denominator
int s To add and subtract the values in the series frequently
int r To print the numerator of the numbers
int k For the loop
int start To take the star ng value
int end To take the last value
int sum To sum the prime numbers
int count For checking the prime numbers
int j For inner loop
int i For outer loop
double x To take the denominator
double su To evaluate the number
12

Ques on 6: Design a class to overload a func on joystring() as follows:

(1) void joystring(String s) with one string argument in lowercase, change the first le er of
every word to uppercase. Display the new string.
(2) void joystring(String str, int p) with one string argument and one integer argument. It
displays all the uppercase character if p is 1(one) otherwise it displays all the lowercase
characters
Solu on:-
public class $6 {
public void joystring(String s)
{
String[] words = s.split(" ");

StringBuilder capitalizedSentence = new StringBuilder();

for (String word : words)


{
capitalizedSentence.append(Character.toUpperCase(word.charAt(0)))

.append(word.substring(1)).append(" ");
}

System.out.println(capitalizedSentence.toString().trim());
}
13

//(2)

public void joystring(String str, int p)


{
StringBuilder result = new StringBuilder();
if (p == 1)
{
for (char c : str.toCharArray())
{
if (Character.isUpperCase(c)) {result.append(c);}
}
}
else { for (char c : str.toCharArray())
{
if (Character.isLowerCase(c))
{
result.append(c);
}
}
}
System.out.println(result.toString());
}
}
Variable descrip on: -

string s To input the string


string word To holdsthe individual words of the input string
string str To input the string
int p To input the integer argument
14

Ques on 7: Write a menu driven program to print the given pa ern accos ng to the user’s
choice.
(i) 1
**
23
***
456
Solu on: -

import java.u l.Scanner;


public class $7
{

public sta c void main(String args[])


{

Scanner sc= new Scanner(System.in);


System.out.println("Enter 1 to print patern 1");
System.out.println("Enter 2 to print patern 2");
int x=sc.nextInt();
if(x==1)
{
int num = 1;

for (int i = 1; i <= 3; i++)


{
for (int j = 1; j <= i; j++)
{
System.out.print(num + " ");
num++;
}
System.out.println();

for (int k = 1; k <= i + 1; k++)


{
System.out.print("* ");
}
System.out.println();
15

}
}

//(ii)A B C D E
// A B C D
// A B C
// A B
// A
if(x==2)
{
char startChar = 'A';

for (int q = 5;q >= 1; q--)


{

for (int a = 0;a < q; a++)


{

System.out.print((char)(startChar + a) + " ");

System.out.println();
}
}
else
{ System.out.println("Invalid input");
}}}
Variable descrip on: -

int num To print the numbers in pa ern 1


int x To input the choice
int i For outer loop in pa ern 1
int j For inner loop and print the series pa ern 1
int k For second inner loop and print Asterisk symbol in pa ern 1
int a For outer loop in pa ern 2
int q For inner loop in pa er 2
char For prin ng the alpabets in pa ern 2
startChar
16

Ques on 8: Write a program to input a sentence and display the word in the sentence that
contains the maximum number of characters
Solu on: -
import java.u l.Scanner;
public class $8
{
public sta c void main(String args[])
{
Scanner sc= new Scanner (System.in);
System.out.println(" Enter the sentence: ");
String l=sc.nextLine();
String[] words = l.split(" ");
String longest = " ";

int maxLength = 0;
for (int i = 0; i < words.length; i++) {
if (words[i].length() > maxLength) {
maxLength = words[i].length();
longest = words[i];
} } System.out.println("The word with the maximum characters is: " + longest); } }
Variable descrip on: -

String l To store the input sentence entered by the user

String[] words To store the words split from the input sentence

String longest To store the longest word found in the sentence

int maxLength To store the length of the longest word

int i To iterate through the words array


17

Ques on 9: Define a class to accept a string and convert the same to uppercase,
create and display the new string by replacing each vowel by immediate next
character and every consonant by the previous character. The other characters
remain the same.

Solu on: -

import java.u l.Scanner;


public class $9 {
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();
String uppercaseString = input.toUpperCase();
StringBuilder newString = new StringBuilder();
for (int i = 0; i < uppercaseString.length(); i++) {
char currentChar = uppercaseString.charAt(i);
if (currentChar == 'A' || currentChar == 'E' || currentChar == 'I' || currentChar ==
'O'||currentChar == 'U') {
newString.append((char)(currentChar + 1));
}
else if (Character.isLe er(currentChar)) {
newString.append((char)(currentChar - 1));
}
else {
newString.append(currentChar);
}
}
System.out.println("The transformed string is: " + newString.toString());
}
}
Variable descrip on: -
String To store the input string entered by the user.
input
String To store the input string converted to uppercase.
uppercaseString
StringBuilder To build the transformed string.
newString
int To iterate through the characters of the uppercase string.
i
char To store the current character being processed in the loop.
currentChar
18

Ques on 10: Write a program in Java to accept a sentence and a word separately. Find and
print the frequency of the given word in the sentence.
Solu on: -
import java.u l.Scanner;
public class $10{
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = sc.nextLine();
System.out.print("Frequency of the word to be searched: ");
String w= sc.nextLine();
String[] wo = s.split(" ");

int fre = 0;
for (int i = 0; i < wo.length; i++) {
if (wo[i].equals(w)) {
fre++;
}
}
System.out.println(" The frequency of the word the is: " + fre);
}
}
Variable descrip on: -

int i An index used in the loop to iterate through the words array
String This variable holds the input sentence provided by the user.
s
String This variable holds the word whose frequency is to be found in the sentence.
w
String[] An array that stores individual words from the input sentence, created by spli ng the sentence on spaces.
wo
int fre An integer variable that keeps track of the number of mes the given word appears in the sentence.
19

Ques on 11: Write a program to input a sentence in uppercase. Display the words which
begin and ends with a same character.
Solu on: -
import java.u l.Scanner;
public class $11 {
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence in uppercase");
String s = sc.nextLine();
String[] wo = s.split(" ");
for (int i = 0; i < wo.length; i++) {
if (wo[i].length() > 1 && wo[i].charAt(0) == wo[i].charAt(wo[i].length() - 1)) {
System.out.print(wo[i]+",");

}
}
}
}
Variable descrip on: -

int i An index used in the loop to iterate through the words array.
String[] An array that stores individual words from the input sentence, created by spli ng the sentence on spaces.
wo
String This variable holds the input sentence provided by the user in uppercase.
s
20

Ques on 12: Define a class to accept the names of 20 students in a one-dimensional array.
Sort the names in ascending order using the Bubble sort technique, and display the
students' names in sorted order.
Solu on: -
import java.u l.Scanner;
public class $12 {
public sta c void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] names = new String[20];
System.out.println("Enter the names of 20 students:");
for (int i = 0; i < 20; i++) {
names[i] = scanner.nextLine(); }
for (int i = 0; i < names.length - 1; i++) {
for (int j = 0; j < names.length - 1 - i; j++) {
if (names[j].compareTo(names[j + 1]) > 0) {
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;}}}
System.out.println("Names of students in ascending order:");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);}}}
Variable descrip on: -

String[] A one-dimensional array that stores the names of 20 students.


names
int i An index used in the outer loop of the Bubble sort to iterate through the array elements
int j An index used in the inner loop of the Bubble sort to compare adjacent elements in the array.
String A temporary variable used to swap the names during the Bubble sort process.
temp
21

Ques on 13: Define a class to accept the marks secured in “Computer Applica ons” by 20
students of a class in a single dimensional array. Sort the marks obtained in descending
order using Selec on sort technique, and display the marks of students in sorted order.
Solu on: -
import java.u l.Scanner;
public class $13 {
public sta c void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] marks = new int[20];
System.out.println("Enter the marks of 20 students in Computer Applica ons:");
for (int i = 0; i < 20; i++) {
marks[i] = scanner.nextInt(); }
for (int i = 0; i < marks.length - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < marks.length; j++) {
if (marks[j] > marks[maxIndex]) {
maxIndex = j;} }
int temp = marks[maxIndex];
marks[maxIndex] = marks[i];
marks[i] = temp; }
System.out.println("Marks of students in descending order:");
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]); } }}

Variable descrip on: -

int i An index used in the outer loop of the Selec on sort to iterate through the array elements.
int temp A temporary variable used to swap elements during the sor ng process.
int j An index used in the inner loop of the Selec on sort to compare elements in the array
int[] A one-dimensional array that stores the marks of 20 students.
marks
int An integer variable used to keep track of the index of the maximum element found
maxIndex in the unsorted part of the array during the Selec on sort process.
22

Ques on 14: Write a program to search for an integer value input by the user in the sorted
list given below using Binary search technique. If found display “Search successful” and the
element, otherwise display “Search Unsuccessful” {31, 36, 45, 48, 50, 51, 67, 78, 89, 90}
Solu on: -
import java.u l.Scanner;
public class $14 {
public sta c void main(String[] args) {
int[] list = {31, 36, 45, 48, 50, 51, 67, 78, 89, 90};
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number to search for: ");
int target = sc.nextInt();
int start = 0;int end = list.length – 1; int index = -1;
while (start <= end) {
int middle = start + (end - start) / 2;
if (list[middle] == target) {
index = middle;
break;
} if (list[middle] < target) {
start = middle + 1; }
else {end = middle - 1;}}
if (index == -1) {
System.out.println("Search Unsuccessful");
} else { System.out.println("Search successful: Element found at index " + index) }}}

Variable descrip on: -

int[] list This array stores the given sorted list of integers.
int target This variable holds the integer value input by the user that needs to be searched in the list.
int start This variable represents the star ng index of the current search range in the binary search
int end This variable represents the ending index of the current search range in the binary search
int middle This variable calculates the middle index of the current search range in the binary search
int index This variable stores the index where the target value is found. It is ini alized to -
1 indicate that the target is not found ini ally.
23

Ques on 15: Define a class to accept values into 4×4 array and find and display the sum of
each row.

1 2 3 4 The Sum of row 1 :10


5 6 7 8
The Sum of row 2 :26
9 10 11 12
13 14 15 16 The Sum of row 3 :42
The Sum of row 4 :58

Solu on: -
import java.u l.Scanner;
public class $15{
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[4][4];
System.out.println("Enter the values into the 4x4 array:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = sc.nextInt(); } }
for (int i = 0; i < 4; i++) {
int sum = 0;
for (int j = 0; j < 4; j++) {
sum += a[i][j];}
System.out.println("The Sum of row " + (i + 1) + " :" + sum); } }}

Variable descrip on: -

int i An index used in the outer loops to iterate through the rows of the array.
int j An index used in the inner loops to iterate through the columns of the array.
int sum A variable used to store the sum of the elements in the current row.
int[][] a A two-dimensional array that stores the values of the 4x4 grid.
24

Ques on 16: Define a class to accept the names of 10 students in an array and check for the
existence of the given name in the array using “Linear search technique”, if found print the
posi on of the name, if not found print the appropriate message. Also print the names
which begins with the word “Mr”
Solu on: -
import java.u l.Scanner;
public class $16 {
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = new String[10];
System.out.println("Enter the names of 10 students:");
for (int i = 0; i < 10; i++) {
names[i] = sc.nextLine(); }
System.out.print("Enter the name to search: ");
String N = sc.nextLine();
int p = -1;
for (int i = 0; i < names.length; i++) {
if (names[i].equals(N)) {
p= i; break; } }
if (p== -1) { System.out.println("Search Unsuccessful: " + N + " not found");}
else { System.out.println("Search successful: " + N + " found at posi on " + (p+ 1)); }
System.out.println("Names beginning with 'Mr':");
for (int i = 0; i < names.length; i++) {
if (names[i].startsWith("Mr")) {
System.out.println(names[i]);}}}}

String[]names A one-dimensional array that stores the names of 10 students.

String N This variable holds the name input by the user that needs to be searched.

int p This variable stores the index where the searchName is found, ini alized to -1.

int i An index used in the loop to iterate through the names array.
25

Ques on 17: Define a class to accept values into a 3×3 array and check if it is a special array.
An array is a special array if the sum of the even elements = sum of the odd elements

4 5 6 Sum of even elements=4+6+2+4+2=18


5 3 2
Sum of even elements=5+5+3+5=18
4 2 5

Solu on: -
import java.u l.Scanner;
public class $17{
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[3][3];
int sE = 0;
int sO = 0;
System.out.println("Enter the values into the 3x3 array:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = sc.nextInt();
if (a[i][j] % 2 == 0) {
sE += a[i][j];
} else {
sO += a[i][j];}} }
if (sE == sO) {System.out.println("The array is a special array.");}
else {System.out.println("The array is not a special array."); }}}
Variable descrip on: -

int[][] a A two-dimensional array that stores the values of the 3x3 grid.

int sE An integer variable used to store the sum of even elements in the array.

int sO An integer variable used to store the sum of odd elements in the array.

int i An index used in the outer loop to iterate through the rows of the array.

int j An index used in the inner loop to iterate through the columns of the array.
26

Ques on 18: Write a program to store the numbers in 4×4 array in double dimensional array
and display the largest and the smallest value within the array.

13 2 23 4 The highest value in the array is :120


52 61 71 18
The smallest value in the array is : 2
95 100 110 120
13 41 15 16

Solu on: -
import java.u l.Scanner;
public class $18 {
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a= new int[4][4];
System.out.println("Enter the values into the 4x4 array:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = sc.nextInt();} }
int min = a[0][0];
int max = a[0][0];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (a[i][j] < min) {min = a[i][j]; }}}
System.out.println("The smallest value in the array is: " + min);
System.out.println("The largest value in the array is: " + max); }}
Variable descrip on: -

int a A two-dimensional array that stores the values of the 4x4 grid.

int min An integer variable used to store the smallest value in the array.

int max An integer variable used to store the largest value in the array.

int i An index used in the outer loop to iterate through the rows of the array.

int j An index used in the inner loop to iterate through the columns of the array.
27

Ques on 19: A book seller maintains records of books belonging to the various
publishers.He uses a class with the specifica ons given below:

Class Name Stock

Data Members

String tle To store the tle of the book

String author To store the author name of the book

String pub To store the publisher of the book

int noc To store the number of copies of the book

Member Func ons

Stock() Default constructor to ini alize all the data members.

void getdata() To accept tle, author, publisher’s name, and the number of copies.

To check the existence of the book by comparing

tle, author's name, and publisher's name. Also checks


void purchase(String t, String a, String p, int n) whether number of noc >= n. If yes, maintain the
balance noc - n, otherwise the message “book is
not available or stock is under flowing”

Solu on: -
import java.u l.Scanner;
public class Stock {
String tle;
String author;
String pub;
int noc;
public Stock() {
tle = "";author = ""
pub = "";noc = 0;}
28

public void getdata() {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the tle of the book: ");


tle = scanner.nextLine();
System.out.print("Enter the author's name: ");
author = scanner.nextLine();
System.out.print("Enter the publisher's name: ");
pub = scanner.nextLine();
System.out.print("Enter the number of copies: ");
noc = scanner.nextInt(); }
public void purchase(String t, String a, String p, int n) {
if ( tle.equals(t) && author.equals(a) && pub.equals(p)) {
if (noc >= n) {noc -= n;
System.out.println("Purchase successful. Remaining copies: " + noc);
} else { System.out.println("Book is not available or stock is insufficient.");
} } else { System.out.println("Book does not exist.");} }
}
public sta c void main(String[] args) {
Stock book = new Stock();
book.getdata();
book.purchase("Example Title", "Example Author", "Example Publisher", 2);
book.purchase("Example Title", "Example Author", "Example Publisher", 1);}
Variable descrip on: -

String tle A String that holds the tle of the book.

String author A String that stores the author's name.

String pub A String that contains the publisher's name.

int noc An int represen ng the number of copies available.


29

Ques on 20: Define a class SmartShop with the specifica ons given below

Members/Func ons Descrip on

String name To store the name of the item purchased

double price To store the price of the item purchased

double netamount To store the net amount paid by the customer a er the discount

SmartShop() Default constructor to ini alize name = "" and price = 0.0

void accept() To input the name and the price of the item using the method of Scanner class

To calculate the net amount to be paid by the customer, based on the discount criteria provided

Price Range Discount Percentage


void calculate() Upto 15000 5.0%
15001 – 25000 8.0%
25001 – 50000 10.0%
More than 50000 12.0%
To display the details in the given format:
void display()
Item Name Item Price Net Amount
xxxxxx xxxxxx xxxxxx

Solu on: -
import java.u l.Scanner;
public class SmartShop {
String name;
double price;
double netamount;
public SmartShop() {name = ""; price = 0.0;}
public void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the item: ");
name = scanner.nextLine();

System.out.print("Enter the price of the item: ");


price = scanner.nextDouble();}
public void calculate() {
30

double discount;
if (price <= 15000)

{discount = 0.05;} else if (price <= 25000)


{discount = 0.08; }
else if (price <= 50000)
{discount = 0.10;}
else {discount = 0.12;}
netamount = price - (price * discount); }
public void display() {
System.out.println("Item Name"+"\t"+"Item Price"+"\t"+"Net Amount");
System.out.println(name +"\t" + price + "\t" + netamount);}
public sta c void main(String[] args) {
SmartShop shop = new SmartShop();
shop.accept();
shop.calculate();
shop.display(); }}
Variable descrip on: -

String name Stores the name of the item purchased

double price Stores the price of the item purchased

double netamount Stores the net amount paid by the customer a er the discount
31

References

 Total Computer Applica ons 9 icse by Morning Star


 Total Computer Applica ons 10 icse by Morning Star
 Microso Copilot
 Microso Word
 My Computer Teacher Sir Noor Islam Khan

You might also like