aarush's_computer_project[1]
aarush's_computer_project[1]
AARUSH
KUMAR
DUTTA
CLASS-10
SECTION-D
1
ROLL NO-
15
INDEX
CONTE PAGE
NTS NO.
1)STUDENT DETAILS 1
2)INDEX 2
3)ACKNOWLEDGEMENT 3
4)INTRODCUTION 4-5
5)ARRAY PROGRAMS 6-15
6)STRING PROGRAMS 16-23
7)FUNCTION PROGRAMS 24-32
8)CONSTRUCTOR PROGRAMS 33-40
9)IF-ELSE PROGRAMS 41-45
10)SWITCH-CASE PROGRAMS 46-52
11)LOOPING PROGRAMS 53-57
12)CONCLUSION 58
2
ACKNOWLEDGE
MENT
I would like to express my sincere gratitude to everyone
who supported me during the development of this
project.
First and foremost, I thank my project supervisor,
Rohan sir, for his valuable guidance, continuous
encouragement, and constructive feedback throughout
3
the project. Their expertise and insights were crucial to
my understanding and execution of the project.
I would also like to extend my appreciation to my
classmates and friends who provided assistance and
motivation during the various stages of this project.
Their collaboration and support made the experience
both enjoyable and educational.
Lastly, I am grateful to my family for their unwavering
support and understanding during the times I
dedicated to this project.
Thank you all for your contributions to making this
project a success.
INTRODUC
TION
In the digital age, programming has become an essential skill that fosters
problem-solving and logical thinking. This project focuses on the development
of a Java application that not only demonstrates the fundamental concepts of
programming but also showcases practical applications that can be beneficial in
real-world scenarios.
4
This application aims to enhance user interaction and provide a seamless
experience while employing Java's robust features, such as object-oriented
programming, exception handling, and data structures.
By undertaking this project, I aim to deepen my understanding of Java
programming and apply theoretical concepts learned in class to create a
functional and user-friendly application. The project covers a wide range of
topics, including arrays, strings, functions, constructors, conditional statements
(if-then-else and switch-case), and loops. The primary focus is on hands-on
implementation through a series of programs designed to enhance
understanding and proficiency in Java programming.
Project Structure:
Arrays (4 Programs):
Searching and sorting operations using integer arrays.
Implementing searching and sorting techniques on character arrays.
Utilizing double arrays for searching and sorting.
Exploring array manipulation and operations.
Strings (4 Programs):
Implementing programs using various string and character functions.
Working with String arrays and exploring their functionalities.
Demonstrating the manipulation of strings in different scenarios.
Functions (3 Programs):
Designing and implementing two normal functions: accept(), calculate().
Developing a display function for output: display().
Introduction to function overloading with more than two functions.
Constructors (2 Programs):
Implementing a normal constructor for initialization.
Exploring constructor overloading with multiple constructors.
5
Implementing a normal if-else-if program.
Developing a slab program to handle various conditions.
Switch-Case (2 Programs):
Creating programs involving numbers using switch-case.
Exploring series programs with switch-case.
Loops (3 Programs):
Implementing patterns using different looping statements.
Creating series programs with various loop structures.
Utilizing loops for numeric operations.
Project Objectives:
Gain hands-on experience in manipulating and managing different data
structures in Java.
Understand the principles of function overloading and constructor
overloading.
Develop problem-solving skills through the implementation of
conditional statements and loops.
Enhance proficiency in using arrays, strings, functions, constructors, and
control flow structures in Java.
6
ARRAY
PROGRAM
S
Q1) Write a program in Java to accept name and marks in the array and sort
the array is ascending order using Bubble Sort.
import java.util.*;
public class BubbleSort
7
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter length no.");
int x = sc.nextInt();
int ar[]=new int[x];
char name[] = new char[x];
int j;
System.out.println("Enter a name");
for( j = 0 ; j<ar.length ; j++)
{
name[j] =sc.next().charAt(0);
}
System.out.println("Enter a marks");
for( j = 0 ; j<ar.length ; j++)
{
ar[j] = sc.nextInt();
}
for( j = 0 ; j<ar.length-1 ; j++)
{
for( int k = 0 ; k<ar.length-j-1 ; k++)
{
if(ar[k]>ar[k+1])
{
int temp = ar[k];
8
ar[k]=ar[k+1];
ar[k+1]=temp;
}
}
}
System.out.println("Sorted array");
for( j = 0 ; j<ar.length ; j++)
{
System.out.println(ar[j]+"\t");
}
}
}
Variable Data Type Description
sc Scanner Scanner object for input
OUTPUT:
9
Q2) Write a program in Java to input and store the weight of ten people.Sort
and display them in descending order using Selection Sort
import java.util.*;
public class Selection
{
public static void main (String args[])
{
int weight[]=new int[10];
Scanner sc = new Scanner(System.in);
int j , k , place, big , temp ;
System.out.println("Input the weight of ten people in the array one by one:");
for(j=0 ; j<10 ; j++)
{
System.out.println("Enter the weight");
10
weight[j]=sc.nextInt();
}
for(j=0 ; j<10-1 ; j++)
{
big=weight[j];
place=j;
for(k= j+1 ; k<10 ; k++)
{
if(weight[k]>big)
{
big=weight[k];
place=k;
}
}
temp=weight[j];
weight[j]=weight[place];
weight[place]=temp;
}
System.out.println("Weights in desending are:");
for(j = 0 ; j < 10 ; j++)
{
System.out.println(weight[j]+"\t");
}
}
}
11
OUTPUT:
12
Q3) Write a program in Java to accept name and marks of the students and
print the name and the marks of the students who scored greater the 90
marks.
import java.util.*;
public class LinearSearch
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter length no.");
int x = sc.nextInt();
int ar[]=new int[x];
String name[] = new String[x];
int j;
System.out.println("Enter a name");
for( j = 0 ; j<ar.length ; j++)
{
name[j] =sc.nextLine();
}
System.out.println("Enter a marks");
for( j = 0 ; j<ar.length ; j++)
{
ar[j] = sc.nextInt();
int flag = 0 ;
13
for( j = 0 ; j <ar.length ; j++)
{
if(ar[j]>90)
{
flag = 1;
break;
}
}
if(flag==1)
{
System.out.println("Name of the Student "+ name[j]+"\t"+"Marks"+ar[j]);
}
}
}
OUTPUT:
14
Q4) Write a program in Java to accept 10 elements in the array and accept
another element check if the element is present in the array or not using
"Binary search Technique" assuming the array is sorted in ascending order.
import java.util.Scanner;
public class BinarySearch
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int arr[] = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};
System.out.print("Enter number to search: ");
int n = in.nextInt();
int l = 0, h = arr.length - 1, index = 1;
while (l <= h) {
int m = (l + h) / 2;
if (arr[m] < n)
l = m + 1;
else if (arr[m] > n)
h = m - 1;
else {
index = m;
break;
}
}
if (index == 1) {
System.out.println("Search element not found");
}
15
else {
System.out.println(n + " found at position " + index);
}
}
}
OUTPUT:
16
STRING
PROGRAM
S
17
Q1) Write a program to accept a word and convert it into lower case, if it is
in upper case. Display the new word by replacing only the vowels with the
letter following it.
import java.util.Scanner;
public class S1
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = in.nextLine();
str = str.toLowerCase();
String newStr = "";
int len = str.length();
for (int i = 0; i<len; i++)
{
char ch = str.charAt(i);
if (str.charAt(i) == 'a' ||str.charAt(i) == 'e' || str.charAt(i) == 'i' ||str.charAt(i)==
'o' || str.charAt(i) == 'u')
{
char nextChar = (char)(ch + 1);
newStr = newStr+ nextChar;
18
}
Else
{
newStr = newStr + ch;
}
}
System.out.println(newStr);
}
}
OUTPUT:
Q2) Write a program in Java to enter a sentence. Display the words which
are only palindrome.
Sample Input: MOM AND DAD ARE NOT AT HOME
Sample Output: MOM
DAD
19
import java.util.Scanner;
if (checkPalin==true )
System.out.println(word);
20
word = "";
}
else {
word += ch;
}
}
}
}
OUTPUT:
21
Sample Input: Computer is Fun
Sample Output: Fun is Computer
import java.util.Scanner;
public class ReverseSentence
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
str = " " + str;
String word = "";
int len = str.length();
for (int i = len - 1; i>= 0; i--) {
char ch = str.charAt(i);
if (ch == ' ') {
System.out.print(word + " ");
word = "";
}
else {
word = ch + word;
}
}
}
}
22
OUTPUT:
23
char ch = in.next().charAt(0);
ch = Character.toUpperCase(ch);
for (int i = 0; i<names.length; i++)
{
if (Character.toUpperCase(names[i].charAt(0)) == ch)
{
System.out.println(names[i]);
}
}
}
}
OUTPUT:
24
25
FUNCTION
PROGRAM
S
Question 4
Anshul transport company charges for the parcels of its customers as per
the following specifications given below:
Class name: Atransport
Member variables:
String name – to store the name of the customer
int w – to store the weight of the parcel in Kg
int charge – to store the charge of the parcel
26
Member functions:
void accept ( ) – to accept the name of the customer, weight of the parcel
from the user (using Scanner class)
void calculate ( ) – to calculate the charge as per the weight of the parcel
as per the following criteria:
import java.util.Scanner;
String name;
int w;
int charge;
name = in.nextLine();
27
w = in.nextInt();
if (w <= 10)
charge = w * 25;
else
System.out.println("Name\tWeight\tBill amount");
System.out.println("----\t------\t-----------");
obj.accept();
obj.calculate();
obj.display();
28
}
OUTPUT:
Member variables:
String name: name of the item purchased
double price: Price of the item purchased
Member methods:
void accept(): Accept the name and the price of the item using the methods
of Scanner class.
void calculate(): To calculate the net amount to be paid by a customer,
based on the following criteria:
29
void display(): To display the name of the item and the net amount to be
paid.
Write the main method to create an object and call the above methods.
import java.util.Scanner;
String name;
double price;
double disc;
double amount;
name = in.nextLine();
price = in.nextDouble();
30
public void calculate() {
double d = 0.0;
d = 0.0;
d = 5.0;
d = 7.5;
d = 10.0;
else
d = 15.0;
31
Eshopobj = new Eshop();
obj.accept();
obj.calculate();
obj.display();
OUTPUT:
32
(a) void series (int x, int n) – To display the sum of the series given below:
x1 + x2 + x3 + .......... xn terms
(c) void series () – To display the sum of the series given below:
long sum = 0;
void series(int p)
33
System.out.println();
void series()
sum += 1.0 / i;
OUTPUT:
34
CONSTRUC
TOR
PROGRAM
S
35
import java.util.*;
BookFair()
Bname=" ";
price=0.0d;
void input()
36
Scanner sc=new Scanner(System.in);
Bname=sc.nextLine();
price=sc.nextDouble();
void calculate()
double dis=0.0;
if(price<=1000)
dis=price*0.02;
dis=price*0.1;
else if(price>3000)
dis=price*1.5;
price=price-dis;
37
void display()
BookFairobj=new BookFair();
obj.input();
obj.calculate();
obj.display();
OUTPUT:
38
Q3) Write a program in Java to calculate the volume with the following
specifications:
Class name - Volume
Data Members: double width, height, depth;
Members Functions:
Volume( double, double, double ) - Parameterized constructor to initialize
width,height and depth
Volume() - non parameterized constructor to initialize the global variables
with default values
Volume(double) - Parameterized constructor to initialize width,height and
depth
volume() - function which returns the product of width, height and depth
Write the main method to call the above constructors with static values for
width, height and depth and print the volume for each case.
public class Volume
{
double width, height, depth;
39
Volume()
{
width = 0;
height = 0;
depth = 0;
}
Volume(double len)
{
width = len;
height = len;
depth = len;
}
double volume()
{
return width * height * depth;
}
public static void main(String args[])
{
Volume myVolume1 = new Volume(10, 20, 15);
Volume myVolume2 = new Volume();
Volume mycube = new Volume(7);
double vol;
40
vol = myVolume1.volume();
System.out.println("Volume of myVolume1 is " + vol);
vol = myVolume2.volume();
System.out.println("Volume of myVolume2 is " + vol);
vol = mycube.volume();
System.out.println("Volume of mycube is "+vol);
}
}
OUTPUT:
41
IF-ELSE
PROGRAM
S
42
Q1) The program also displays whether the numbers entered by the user are
'All positive', 'All negative' or 'Mixed numbers'.
Sample Input: 56, -15, 12
Sample Output:
The greatest number is 56
Entered numbers are mixed numbers.
import java.util.Scanner;
public class If_else1
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
int greatestNumber = a;
if ((a == b) && (b == c)) {
System.out.println("Entered numbers are equal.");
}
else {
43
if (b >greatestNumber) {
greatestNumber = b;
}
if (c >greatestNumber) {
greatestNumber = c;
}
System.out.println("The greatest number is " + greatestNumber);
if ((a >= 0) && (b >= 0) && (c >= 0)) {
System.out.println("Entered numbers are all positive numbers.");
}
else if((a < 0) && (b < 0) && (c < 0)) {
System.out.println("Entered numbers are all negative numbers.");
}
else {
System.out.println("Entered numbers are mixed numbers.");
}
}
}
}
OUTPUT:
44
Q2) Define a class ElectricBill with the following specifications:
class :ElectricBill
Member methods:
void accept( ) — to accept the name of the customer and number of units
consumed
void calculate( ) — to calculate the bill as per the following tariff:
45
Write a main method to create an object of the class and call the above
member methods.
import java.util.Scanner;
46
bill = amt + surcharge;
}
}
OUTPUT:
47
SWITCH
CASE
48
PROGRAM
S
49
For example,
3, 8, 15, 24, 35,......................are sunny numbers.
For an incorrect option, an appropriate error message should be displayed.
import java.util.Scanner;
switch (ch)
{
case 1:
int a = 1, b = 1, c = 2;
50
b = c;
c = term;
}
break;
case 2:
System.out.print("Enter n: ");
int n = in.nextInt();
51
OUTPUT:
S = x1 - x2 + x3 - x4 + x5 - ....................... - x 20
import java.util.Scanner;
52
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Sum of x^1 - x^2 + .... - x^20");
System.out.println("2. Sum of 1/a^2 + 1/a^4 + .... to n terms");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
System.out.print("Enter the value of x: ");
int x = in.nextInt();
long sum1 = 0;
for (int i = 1; i<= 20; i++) {
int term = (int)Math.pow(x, i);
if (i % 2 == 0)
sum1 -= term;
else
sum1 += term;
}
System.out.println("Sum = " + sum1);
break;
case 2:
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
53
System.out.print("Enter the value of a: ");
int a = in.nextInt();
int c = 2;
double sum2 = 0;
for(int i = 1; i<= n; i++) {
sum2 += (1/Math.pow(a, c));
c += 2;
}
System.out.println("Sum = " + sum2);
break;
default:
System.out.println("Incorrect choice");
break;
}
}
OUTPUT:
54
55
LOOPING
PROGRAM
S
Q1)
56
public class Pattern1
{
public void displayPattern()
{
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();
}
}
}
OUTPUT:
57
Q2) A special two-digit number is such that when the sum of its digits is
added to the product of its digits, the result is equal to the original two-digit
number.
Write a program to accept a two-digit number. Add the sum of its digits to
the product of its digits. If the value is equal to the number input, then
display the message "Special two—digit number" otherwise, display the
message "Not a special two-digit number".
import java.util.Scanner;
58
int count = 0, digitSum = 0, digitProduct = 1;
while (num != 0) {
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
if (count != 2)
else
59
OUTPUT:
60
CONCLUSI
ON
In conclusion, this project has provided an invaluable
opportunity to explore the intricacies of Java programming
while developing a practical application. The process of
designing, coding, and testing has not only strengthened my
technical skills but also enhanced my problem-solving abilities
and attention to detail. This project has illustrated the
importance of careful planning and structured development in
creating functional software solutions. Furthermore, it
emphasizes the significant role of technology in addressing
real-world challenges and improving efficiency in various
domains. I am confident that the skills and knowledge acquired
through this project will serve as a solid foundation for my
future endeavours in programming and software development.
61