0% found this document useful (0 votes)
6 views26 pages

Computer X Class Board Project

The document outlines various programming tasks for Class X, including calculating square and cube roots, managing a parking lot system, determining admission streams based on marks, checking for Niven numbers, and implementing menu-driven programs for patterns. It also includes programs for counting vowels and consonants, searching for city names, calculating volumes of different shapes, and managing book pricing with discounts. Each task is accompanied by code examples and variable descriptions for clarity.

Uploaded by

muzaina934
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)
6 views26 pages

Computer X Class Board Project

The document outlines various programming tasks for Class X, including calculating square and cube roots, managing a parking lot system, determining admission streams based on marks, checking for Niven numbers, and implementing menu-driven programs for patterns. It also includes programs for counting vowels and consonants, searching for city names, calculating volumes of different shapes, and managing book pricing with discounts. Each task is accompanied by code examples and variable descriptions for clarity.

Uploaded by

muzaina934
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/ 26

CLASS X PROJECT PROGRAMS

1. Write a program to input a number. Calculate its square root and cube root. Finally,
display the result by rounding it off.

import java.util.Scanner;

public class MathRoots


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in); //creating scanner object

System.out.print("Enter the number: ");// inserting the number


int num = in.nextInt();

double sqrtVal = Math.sqrt(num);


double cbrtVal = Math.cbrt(num);
long roundSqrt = Math.round(sqrtValue);
long roundCbrt = Math.round(cbrtValue);

System.out.println("Square root of " + num + " = " + sqrtVal);


System.out.println("Rounded form = " + roundSqrt);
System.out.println("Cube root of " + num + " = " + cbrtVal);
//displaying cube root of a number
System.out.println("Rounded form = " + roundCbrt);
}
}
Output:

2. Define a class called ParkingLot with the following description:

Write a main method to create an object of the class and call the above methods.
Data Members Purpose
int vno To store the vehicle number
int hou rs To store the num ber of hours the vehicle is parked
in the parking lot
double bill To store the bill amount

Member Methods Purpose


void input( ) To input the vno and hours
void calculate( ) To com pute the parking charge at the rate ₹3 for
the first hour or the part thereof and ₹1.50 for
each additional hour or part thereof.
void display() To display the detail

import java.util.Scanner;

public class ParkingLot

Private int vno; //To store vehicle number

private int hours;

private double bill; //To store bill amount

public void input() { //To input vno and hours

Scanner in = new Scanner(System.in);

System.out.print("Enter vehicle number: ");

vno = in.nextInt();

System.out.print("Enter hours: ");

hours = in.nextInt();

public void calculate() {

if (hours <= 1)
bill = 3;

else

bill = 3 + (hours - 1) * 1.5;

public void display() { //To display the details

System.out.println("Vehicle number: " + vno);

System.out.println("Hours: " + hours);

System.out.println("Bill: " + bill);

public static void main(String args[]) {

ParkingLot obj = new ParkingLot();

obj.input();

obj.calculate();

obj.display();

}} //closing main and class

Variable Descriptive Table:


Variable Name Data Type Description
vno int To store the vehicle number
hours int To store the number of hours the
vehicle is parked in the parking
lot
bill double To store the bill amount

3. An ICSE school displays a notice on the school notice board regarding admission in Class XI for
choosing stream according to marks obtained in English, Maths and Science in Class 10 Council
Examination.

Marks obtained in different subjects Stream


Eng, Maths and Science >= 80% Pure Science
Eng and Science >= 80%, Maths >= 60% Bio. Science
Eng, Maths and Science >= 60% Commerce
Print the appropriate stream allotted to a candidate. Write a program to accept marks in English, Maths
and Science from the console.
import java.util.Scanner;

public class Admission


{ //class open
public static void main (String args[]) {

Scanner in = new Scanner(System.in); //scanner object created

System.out.print("Enter marks in English: ");//entering subject marks


int eng = in.nextInt();

System.out.print("Enter marks in Maths: ");


int maths = in.nextInt();

System.out.print("Enter marks in Science: ");


int sci = in.nextInt();

if (eng >= 80 && sci >= 80 && maths >= 80) //condition check
System.out.println("Pure Science");
else if (eng >= 80 && sci >= 80 && maths >= 60)
System.out.println("Bio. Science");
else if (eng >= 60 && sci >= 60 && maths >= 60)
System.out.println("Commerce");
else
System.out.println("Cannot allot any stream");
} //main close
}
}
Variable Descriptive Table:
Variable Name Data Type Description
eng int To store English marks
maths int To store maths marks
sci int To store science marks

4. Write a program to input a number. Check and display whether it is a Niven number or not. (A
number is said to be Niven which is divisible by the sum of its digits).

Example: Sample Input 126


Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.
import java.util.*; //importing util package

public class NivenNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in); //creating scanner class object
System.out.print("Enter number: ");
int num = in.nextInt();
int orgNum = num;

int digitSum = 0;

while (num != 0) {
int digit = num % 10; num
= num/ 10;
digitSum =digitSum + digit; //program logic
}

/*
* digitSum != 0 check prevents
* division by zero error for the
* case when users gives the number
* 0 as input
*/
if (digitSum != 0 && orgNum % digitSum == 0)
System.out.println(orgNum + " is a Niven number");//Output display
else
System.out.println(orgNum + " is not a Niven number");
}
} //main close
Output

Variable Descriptive Table:

Variable Name Data Type Description num int To insert the number
orgNum int To store num value as original
value
digitSum int To store sum of digits
digit int To store the modulated value of
num

5. Using the switch statement, write a menu driven program for the following:
(a) To print the Floyd's triangle:
12 34 5 6
7 8 9 10
11 12 13 14 15

(b) To display the following pattern:


I
IC
ICS
ICSE

For an incorrect option, an appropriate error message should be displayed.


import java.util.Scanner; //importing scanner class of util package

public class FloydPattern


{ public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println(“Type 1 for Floyd’s triangle”);
System.out.println(“Type 2 for an ICSE pattern”);

System.out.print(“Enter your choice: “);


int ch = in.nextInt(); //Taking the choice of menu

switch (ch) { case 1:


int a = 1; for (int i = 1; i <= 5; i++)
{ for (int j = 1; j <= i; j++) {
System.out.print(a++ + “\t”);
}
System.out.println();
}
break;

case 2:
String s = “ICSE”;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + “ “);
}
System.out.println();
}
break;
default:
System .out .println( “Incorrect Choice ”);
} //closing of switch
} //closing of main
}

Output

Variab le Descriptive Table:

Variable Name Data Type Description


ch int To enter the choice
a int To store num value
s String To store the string
i int To insert number of rows

j int To insert number of columns

6. Write a program to input a set of 20 letters. Convert each letter into upper case. Find and display the
number of vowels and number of consonants present in the set of given letters.
import java.util.Scanner;

public class LetterSet


{
public static void main(String args[]) { //main
Scanner in = new Scanner(System.in);
System.out.println("Enter any 20 letters");//inserting the letters
int vc = 0, cc = 0;
for (int i = 0; i < 20; i++) {
char ch = in.next().charAt(0);
ch = Character.toUpperCase(ch); //check and convertion of charter to uppercase
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' ||

ch == 'U')
vc++;
else if (ch >= 'A' && ch <= 'Z')
cc++; }
System.out.println("Number of Vowels = " + vc);
System.out.println("Number of Consonants = " + cc);
}
} //cosing of class

Output

Variable Descriptive Table:


Variable Name Data Type Description
vc int To store counted vowels
cc int To store counted consonant
ch char To store the character
i int To insert number of letters

7. Write a program to accept the year of graduation from school as an integer value from the user.
Using the binary search technique on the sorted array of integers given below, output the message
"Record exists" if the value input is located in the array. If not, output the message "Record does not
exist".
Variable Name Data Type Description
n[] int Single dimensional array
year int To store year
l int To store first index position
h int To store last index position

idx int To store the exit index of an


array
m int Binary search technique is stored
8. Write a program to accept the names of 10 cities in a single dimensional string array and their STD
(Subscribers Trunk Dialling) codes in another single dimension integer array. Search for the name of a
city input by the user in the list. If found, display "Search Successful" and print the name of the city
along with its STD code, or else display the message "Search unsuccessful, no such city in the list".
import java.util.Scanner;

public class StdCodes


{
public static void main(String args[]) { //main int
SIZE = 10;
Scanner in = new Scanner(System.in);
String cities[] = new String[SIZE];
String stdCodes[] = new String[SIZE];
System.out.println("Enter " + SIZE + " cities and their STD codes:");

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


System.out.print("Enter City Name: ");
cities[i] = in.nextLine();
System.out.print("Enter its STD Code: ");
stdCodes[i] = in.nextLine();
}

System.out.print("Enter name of city to search: ");


String city = in.nextLine();

int idx;
for (idx = 0; idx < SIZE; idx++) {
if (city.compareToIgnoreCase(cities[idx]) == 0) { //citites comparision
break;
}
}

if (idx < SIZE) {


System.out.println("Search Successful"); //output display
System.out.println("City: " + cities[idx]);
System.out.println("STD Code: " + stdCodes[idx]);
}
else {
System.out.println("Search Unsuccessful");
}
} //main close
}
Output

Variable Descriptive Table:

Variable Name Data Type Description


SIZE int Array Size of cities and code
cities[] String To store array of cities name
stdCodes[] String To store std code
city String To store city name

idx int To store the exit index of an


array
9. Design a class to overload a function volume( ) as follows:

1. double volume(double r) — with radius (r) as an argument, returns the volume of sphere using
the formula:
V = (4/3) * (22/7) * r * r * r
2. double volume(double h, double r) — with height(h) and radius(r) as the arguments, returns the
volume of a cylinder using the formula: V = (22/7) * r * r * h
3. double volume(double 1, double b, double h) — with length(l), breadth(b) and height(h) as the
arguments, returns the volume of a cuboid using the formula: V = l*b*h (length * breadth *
height)
public class Volume
{
double volume(double r) {
return (4 / 3.0) * (22 / 7.0) * r * r * r;
} //volume calculation
double volume (double h, double r) {
return (22 / 7.0) * r * r * h;
}

double volume (double l, double b, double h) {


return l * b * h;
}

public static void main (String args []) {


Volume obj = new Volume(); //object of volume class created
System .out .println( "Sphere Volume = " +
obj .volume( 6));
System .out .println( "Cylinder Volume = " +
obj .volume( 5, 3.5));
System .out .println( "Cuboid Volume = " + //display cuboid volume
obj .volume( 7.5, 3.5 , 2));
}
} //closing of class

Outpu t

Va riable Descriptive Table:

Variable Name Data Type Description


r double To store radius
h double To store height
l double To store length
b double To store breadth

volume double To store calculated volume


10. Define a class called BookFair with the following description:

Data Members Purpose


String Bname stores the name of the book
double price stores the price of the book
Member Meth ods Purpose
BookFair( ) Constructor to initialize data members
void input( ) To input and store the name and price of the book
void calculate ( ) To calculate the price after discount. Discount is
calculated as per the table given below
void display( ) To display the name and price of the book after
discount

Price Discount
Less than or equal to Rs 1000 2% of price
More than Rs1000 and less than or equal to 10% of price
Rs3000
More than Rs3000 15% of price
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;

public class BookFair


{
private String bname; // stores the name of the book
private double price;

public BookFair() { // Constructor to initialize data members


bname = ""; price = 0.0;
}

public void input() { // To input and store the name and price of the book
Scanner in = new Scanner(System.in);
System.out.print("Enter name of the book: ");
bname = in.nextLine();
System.out.print("Enter price of the book: ");
price = in.nextDouble();
}

public void calculate() {


double disc; if
(price <= 1000) disc
= price * 0.02; else if
(price <= 3000) disc
= price * 0.1; else
disc = price * 0.15;

price -= disc;
}

public void display() { //displaying the details of the book


System.out.println("Book Name: " + bname);
System.out.println("Price after discount: " + price);
}

public static void main(String args[]) {


BookFair obj = new BookFair(); //creating object of BookFair class
obj.input(); obj.calculate(); obj.display();
}
}

Variable Descriptive Table:


Variable Name Data Type Description
bname String To store book name
price double To store book price
disc double To store the calculated discount
11. Write the pattern program

1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
4 5 6 7
5 6 7
6 7
7
6 7
5 6 7
4 5 6 7
3 4 5 6 7
2 3 4 5 6 71
2 3 4 5 6 7

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern....!!!");

//Printing upper half of the pattern

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


{
//Printing i spaces at the beginning of each row

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


{
System.out.print(" ");
}

//Printing i to rows value at the end of each row

for (int j = i; j <= rows; j++)


{
System.out.print(j+" ");
}

System.out.println();
}
//Printing lower half of the pattern

for (int i = rows-1; i >= 1; i--)


{
//Printing i spaces at the beginning of each row

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


{
System.out.print(" ");
}

//Printing i to rows value at the end of each row

for (int j = i; j <= rows; j++)


{
System.out.print(j+" ");
}

System.out.println();
}

}//closing main
}
Output:
Same as pattern

Variable Descriptive Table:

Variable Name Data Type Description rows int To enter no of rows i int To print no of
rows
j int To print no of columns
12. Write a program in Java to accept a name(Containing three words) and Display only the initials
(i.e., first letter of each word).
Sample Input: Nalanda Public School
Sample Output: N P S
import java.util.Scanner;

public class NameInitials


{
public static void main(String args[]){
Scanner in = new Scanner(System.in); //scanner object created
System.out.println("Enter a name of 3 or more words:");// entering the name
String str = in.nextLine();

System.out.print(str.charAt(0) + " ");


for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == ' ')
System.out.print(str.charAt(i + 1) + " ");//displaying string
}
} //close main
}
Output:

Enter a name of 3 or more words:

Nalanda Public School N P S

Variable Descriptive Table:

Variable Name Data Type Description str String To enter name i int Condition check
13. Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending order and
next the 10 numbers in descending order by using 'Bubble Sort' technique. Finally, print the complete
list of integers.
import java.util.Scanner;

public class BubbleSort


{ public static void main (String args[]){
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");
for (int i = 0; i < arr.length; i++) { arr[i] = in.nextInt();
}

//Sort first half in ascending order for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Sort second half in descending order for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Print the final sorted array


System.out.println("\nSorted Array:"); for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output:

Variable Descriptive Table:


Variable Name Data Type Description
arr[] int To store string array
i int To print/store no of rows
j int To print /store no of columns
14.

Write a menu driven program to generate the upper case letters from Z to A and lower case letters from
'a' to 'z' as per the user's choice.
Enter '1' to display upper case letters from Z to A and Enter '2' to display lower case letters from a to z.
import java.util.Scanner;

public class Letters


{
public static void main (String args[]){

Scanner in = new Scanner(System.in);


System.out.println("Enter '1' to display upper case letters from Z to A");
System.out.println("Enter '2' to display lower case letters from a to z");

System.out.print("Enter your choice: ");


int ch = in.nextInt();
int count = 0;

switch (ch) {

case 1:
for (int i = 90; i > 64; i--) { //program logic
char c = (char)i;
System.out.print(c);
System.out.print(" ");
count++;

//Print 10 characters per line


if (count == 10) {
System.out.println();
count = 0;

} }
break;

case 2:
for (int i = 97; i < 123; i++) {
char c = (char)i;
System.out.print(c);
System.out.print(" ");
count++;

//Print 10 characters per line


if (count == 10) {
System.out.println();
count = 0;

} }
break;

default:
System.out.println("Incorrect Choice");
}
}
} //close main
Output:

Variable Descriptive Table:


Variable Name Data Type Description
ch int To store choice
count int To store initial value of an array
i int To s tore the array
c char To store character value
15. Write the programs to find the sum of the following series:

S = a + a 2 + a3 + ....... + a n

import java.util.Scanner;

public class Series


{
Public static void main(Stirng args[]) {

Scanner in = new Scanner(System.in); //scanner object creted


System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");//entering power value
int n = in.nextInt(); int sum = 0;

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


sum += Math.pow(a, i);
}

System.out.println("Sum=" + sum); //displaying the output


}
} //main close

Output:

Variable Descriptive Table:


Variable Name Data Type Description
a int To store a value
n int To store power value
sum int To store the series sum
16. A double dimensional array is defined as N[4][4] to store numbers. Write a program to find the
sum of all even numbers and product of all odd numbers of the elements stored in Double
Dimensional Array (DDA).
import java.util.Scanner;

public class DDAEvenOdd


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N[][] = new int[4][4];
long evenSum = 0, oddProd = 1;
System.out.println("Enter the elements of 4x4 DDA: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
N[i][j] = in.nextInt();
if (N[i][j] % 2 == 0)
evenSum += N[i][j];
else
oddProd *= N[i][j];
}
}

System.out.println("Sum of all even numbers = " + evenSum);


System.out.println("Product of all odd numbers = " + oddProd);
}
}

You might also like