Computer projectX
Computer projectX
Class : X-B
Subject : Computer
SUBMITTED TO-
Acknowledgement :
programming concepts.
class NonStaticMethodExample {
public int cube(int number) {
return number * number * number;
}
public static void main(String[] args) {
NonStaticMethodExample example = new NonStaticMethodExample();
int num = 4;
int result = example.cube(num);
System.out.println("The cube of " + num + " is: " + result);
}
} Output of Program 2:
The cube of 4 is: 64
2. Design a class with the following specifications:Class name: Student
Member variables:
name — name of studentage — age of studentmks — marks obtainedstream
— stream allocated
(Declare the variables using appropriate data types)Member methods:void
accept() — Accept name, age and marks using methods of Scanner
class.void allocation() — Allocate the stream as per following criteria:mks,
stream
>= 300, Science and Computer
>= 200 and < 300, Commerce and Computer
>= 75 and < 200, Arts and Animation
< 75, Try Again
import java.util.*;
String name;
int age;
int mks;
String stream;
name = sc.nextLine();
age = sc.nextInt();
mks = sc.nextInt();
} else {
student.accept();
student.allocation();
student.print();
Output:-
Variable Data
Enter student name: John Doe Description
Name Type
Enter student age: 18
name String Holds the name of the student.
Enter marks obtained: 250
age int Stores the age of the student.
Student Name: John Doe
Stores the marks obtained by the
mks int
Student Age: 18 student.
import java.util.Scanner;
public class StringAnalysis {
// Member variables to store the counts
private String inputString;
private int digitsCount;
private int alphabetsCount;
private int specialCharsCount;
public void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
inputString = sc.nextLine();
}
public void analyze() {
digitsCount = 0;
alphabetsCount = 0;
specialCharsCount = 0;
for (int i = 0; i < inputString.length(); i++) {
char ch = inputString.charAt(i);
if (Character.isDigit(ch)) {
digitsCount++;
}
else if (Character.isLetter(ch)) {
alphabetsCount++;
}
else {
specialCharsCount++;
}
}
}
public void print() {
System.out.println("Number of digits: " + digitsCount);
System.out.println("Number of Alphabets: " + alphabetsCount);
System.out.println("Number of Special characters: " +
specialCharsCount);
}
Output :
Enter a string: Hello@123!
Number of digits: 3
Number of Alphabets: 5
Number of Special characters: 2
Data
Variable Name Description
Type
import java.util.Scanner;
if (word.length() > 0) {
word.substring(1);
result.append(capitalizedWord).append(" ");
return result.toString().trim();
}
For The Input: Enter a string in lowercase: we are in cyber world
The Output Will Be : Converted String: We Are In Cyber World
Variable Description:
inputString String Stores the input string entered by the user in lowercase.
capitalizedWord String Holds each word after capitalizing the first letter.
9.Write a program to accept a number and check and display whether it is a spy
number or not. (A number is spy if the sum of its digits equals the product of its
digits.)Example: consider the number 1124.
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 x 1 x 2 x 4 = 8
import java.util.Scanner;
public class SpyNumber {
public static boolean isSpyNumber(int number) {
int sum = 0;
int product = 1;
while (number > 0) {E
int digit = number % 10;
sum += digit; // Add the digit to sum
product *= digit; // Multiply the digit to product
number /= 10; // Remove the last digit
}
return sum == product;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
if (isSpyNumber(number)) {
System.out.println(number + " is a Spy Number.");
} else
{
System.out.println(number + " is not a Spy Number.");
}
}
}
Variable Data
Description
Name Type
number.
import java.util.Scanner;
import java.util.Scanner;
public class ReverseString
{
public static String reverse(String input) {
StringBuilder reversed = new StringBuilder(input);
return reversed.reverse().toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String reversedString = reverse(inputString);
System.out.println("Reversed string: " + reversedString);
}
}
Input :
Enter a string: Hello World
The output will be:
Reversed string: dlroW olleH
Variable Description:
Variable Name Data Type Description
inputString String Stores the input string provided by the user.
reversedString String Stores the reversed version of the input string.
sc ScannerObject used to accept user input from the console.
reversedStringBuilder Used to manipulate and reverse the input string
efficiently.
19. Write a program to search for an element in an array using linear search.
import java.util.Scanner;
public class LinearSearch {
public static int linearSearch(int[] array, int target) {
for (int i = 0; i < array.length; i++) {
if (array[i] == target) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = sc.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < size; i++) {
array[i] = sc.nextInt();
}
System.out.print("Enter the element to search for: ");
int target = sc.nextInnt();
int resultIndex = linearSearch(array, target);
if (resultIndex != -1) { System.out.println("Element " + target + " found at index: " +
resultIndex);
}
else { System.out.println("Element " + target + " not found in the array.");
}}}
For the input:
Enter the size of the array: 5
Enter the elements of the array:
1
3
5
7
9
Enter the element to search for: 5
Variable Description:
array int[] An array of integers to store the elements entered by the user.
target int The element that the user wants to search for in the array.
resultIndex int The index of the target element if found, or -1 if not found.
23.Write a menu driven program to display the pattern as per user’s choice.
Pattern 1
ABCDE
ABCD
ABC
AB
A
Pattern 2
B
LL
UUU
EEEE
import java.util.Scanner;
switch (choice) {
case 1:
System.out.println("Pattern 1:");
pattern1();
break;
case 2:
System.out.println("Pattern 2:");
pattern2();
break;
case 3:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice. Please choose again.");
}
System.out.println();
} while (choice != 3);
}
}
Variable Description:
Variable Name Data Type Description
choice int Stores the user's menu choice.
sc Scanner An object to read user input from the console.
24. Write a program to accept a number and check and display whether it is a
Niven number or not.
(Niven number is that number which is divisible by its sum of digits.).
Example:
Consider the number 126. The sum of its digits is 1 + 2 + 6 = 9 and
126 is divisible by 9.
import java.util.Scanner;
import java.util.Scanner;
Variable Description:
6. Write a program to input a number and check and print whether it is a Pronic
number or not. (Pronic number is the number which is the product of two
consecutive integers)Examples:12 = 3 x 420 = 4 x 542 = 6 x 7
import java.util.Scanner;
.
Variable Description:
Variable Name Data Type Description
number int The input number provided by the user.
A loop variable used to check for consecutive
i int
integers.
sc Scanner An object used to read user input from the console.
Output :
Volume of the sphere with radius 5.00 is 523.81
Volume of the cylinder with height 10.00 and radius 3.00 is 188.57
Volume of the cuboid with length 4.00, breadth 5.00, and height 6.00 is 120.00
17. Write a program to input a sentence from the user and count the number of
letters and spaces present in it.
import java.util.Scanner;
public class SentenceAnalysis {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
Variable Description:
Variable Name Data Type Description
sentence String The input sentence provided by the user.
letterCount int To store the count of letters in the sentence.
spaceCount int To store the count of spaces in the sentence.
sc Scanner An object used to read user input from the console.
18. Write a program to search for an element in an array using binary search
import java.util.Scanner;
if (result == -1) {
System.out.println("Element not found in the array.");
} else {
System.out.println("Element found at index: " + result);
}
}
}
Variable Description:
Variable Data
Description
Name Type
n int The number of elements in the array.
arr int[] The array of sorted integers provided by the user.
The element that the user wants to search for in the
target int
array.
result int The index of the found element (or -1 if not found).
sc Scanner An object used to read user input from the console.
left int The starting index of the current search range.
right int The ending index of the current search range.
mid int The middle index of the current search range.
20. Define a class to accept values in integer array of size 10. Sort them in an
ascending order using selection sort technique. Display the sorted array.
import java.util.Scanner;
public class SelectionSort {
private int[] arr = new int[10];
public void acceptValues() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 10 integers:");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
}
public void selectionSort() {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i; // Assume the minimum is the first element
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j; // Update minIndex if a smaller element is found
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public void displaySortedArray() {
System.out.println("Sorted array in ascending order:");
for (int value : arr) {
System.out.print(value + " ");
}
}
public static void main(String[] args) {
SelectionSort sorter = new SelectionSort();
sorter.acceptValues();
sorter.selectionSort();
sorter.displaySortedArray();
}}
Variable Description:
Variable Data
Description
Name Type
arr int[] An array of integers of size 10 to store the user inputs.
sc Scanner An object used to read user input from the console.
The index of the minimum element found in the
minIndex int
unsorted portion of the array.
A temporary variable used for swapping elements
temp int
during sorting.
i, j int Loop variables used for iterating through the array.
10. Write a program to input integer elements into an array of size 20 and
perform the following operations:Display largest number from the array.Display
smallest number from the array.Display sum of all the elements of the array.
import java.util.Scanner;
public class ArrayOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[20]; // Array of size 20
System.out.println("Enter 20 integer elements:");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt(); // Store user input in the array
}
int largest = arr[0]; // Initialize largest with the first element
int smallest = arr[0]; // Initialize smallest with the first element
int sum = 0; // Initialize sum to 0
for (int num : arr) {
if (num > largest) {
largest = num
}
if (num < smallest) {
smallest = num;
}
sum += num;
}
System.out.println("Largest number: " + largest);
System.out.println("Smallest number: " + smallest);
System.out.println("Sum of all elements: " + sum);
}
}
For the input: The output will be:
Enter 20 integer elements: Largest number: 90
12 Smallest number: 0
5 Sum of all elements:
480
23
34
56
2
89
45
90
1
76
34
22
8
19
0
11
27
49
38
Variable Description:
Variable
Data Type Description
Name
An array of integers of size 20 to store the user
arr int[]
inputs.
Variable
Data Type Description
Name
Variable to store the largest number found in the
largest int
array.
Variable to store the smallest number found in the
smallest int
array.
sum int Variable to store the sum of all elements in the array.
sc Scanner An object used to read user input from the console.
num int Loop variable used to iterate through the array.
12. Write a program to perform binary search on a list of integers given below,
to search for an element input by the user. If it is found display the element
along with its position, otherwise display the message "Search element not
found".5, 7, 9, 11, 15, 20, 30, 45, 89, 97.
import java.util.Scanner;
Variable Description:
Variable Data
Description
Name Type
A predefined array of sorted integers for binary
arr int[]
search.
The element that the user wants to search for in the
target int
array.
result int The index of the found element (or -1 if not found).
sc Scanner An object used to read user input from the console.
Variable Data
Description
Name Type
left int The starting index of the current search range.
right int The ending index of the current search range.
mid int The middle index of the current search range.
import java.util.Scanner;
class FruitJuice {
int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;
public FruitJuice()
this.product_code = 0;
this.flavour = " ";
this.pack_type = " ";
this.pack_size = 0;
this.product_price = 0;
}
// Method to input and store the product details
public void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter product code: ");
product_code = sc.nextInt();
sc.nextLine();
System.out.print("Enter flavour of the juice: ");
flavour = sc.nextLine();
System.out.print("Enter pack type: ");
pack_type = sc.nextLine();
System.out.print("Enter pack size (in mL): ");
pack_size = sc.nextInt();
System.out.print("Enter product price: ");
product_price = sc.nextInt();
}
public void discount() {
product_price -= 10;
}
public void display() {
System.out.println("Product Code: " + product_code);
System.out.println("Flavour: " + flavour);
System.out.println("Pack Type: " + pack_type);
System.out.println("Pack Size: " + pack_size + " mL");
System.out.println("Product Price: " + product_price);
}
public static void main(String[] args) {
FruitJuice juice = new FruitJuice();
juice.input();
juice.discount();
juice.display();
}
}
class PrintOverloading {
void print() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 4; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
void print(int n) {
int oddSum = 0;
int evenSum = 0;
int originalNumber = n;
while (n > 0) {
int digit = n % 10; // Extract the last digit
if (digit % 2 == 0) {
evenSum += digit; // Sum of even digits
} else {
oddSum += digit; // Sum of odd digits
}
n /= 10; // Remove the last digit
}
if (oddSum == evenSum) {
System.out.println(originalNumber + " is a lead number.");
} else {
System.out.println(originalNumber + " is not a lead number.");
}
}
1111 po.print(numberToCheck);
2222 }}
3333
4444
5555
3669 is a lead number.
Variable Description:
Variable Name Data Type Description
oddSum int Stores the sum of odd digits of the number.
evenSum int Stores the sum of even digits of the number.
Holds the last digit extracted from the
digit int
number during iteration.
Stores the original value of the input number
originalNumber int
for display.
The number passed to check if it is a lead
n int
number.
An instance of the PrintOverloading class
po PrintOverloading
used to call the methods.
Bibliography :