0% found this document useful (0 votes)
23 views46 pages

Java Programming Projects for Class 9

This document contains a computer project report submitted by Roshan Sharma for their class 9 topic on Java programming. The report includes 12 questions on Java programming concepts with sample code and output for each. It also includes an acknowledgment section thanking those who helped with the project like their teacher, principal, and parents.

Uploaded by

Jasdev Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views46 pages

Java Programming Projects for Class 9

This document contains a computer project report submitted by Roshan Sharma for their class 9 topic on Java programming. The report includes 12 questions on Java programming concepts with sample code and output for each. It also includes an acknowledgment section thanking those who helped with the project like their teacher, principal, and parents.

Uploaded by

Jasdev Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Computer

Project

Name-Roshan Sharma
Class-9
Roll no-17
Topic-25 Java programming
School-DSM school for
excellence
Given by-Arijit Sir
Content
Serial no Topic Page
no
1. Acknowledgment 2
3. If else 3
4. Else if 14
5. For loop 30
6. While loop 36
7. Bibliography 41

1
Acknowledgment
I am over helmed in all humbleness and
gratefulness to acknowledge my depth to all
those who have helped me to put these ideas,
well above the level of simplicity and into
something concrete.
I would like to express my special thanks of
gratitude to my teacher as well as our principal
who gave me the golden opportunity to do this
wonderful project on the topic Java programing,
which also helped me in doing a lot of Research
and i came to know about so many new things. I
am really thankful to them.
I would like to thank my parents who helped me
a lot in gathering different information,
collecting data and guiding me from time to
time in making this project, despite of their busy

2
schedules, they gave me different ideas in
making this project unique.

Q1 WAP to input cost price and selling price


of a product. If S.P is more than C.P then
calculate the profit and profit percent
otherwise calculate and display actual loss
and loss percent.
Program:-
import [Link].*;
class main
{
public static void main(String[]args)throws
IOException
{

3
BufferedReader in =new
BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter the C.P and
S.P :");
int a,b, profit=0,loss=0;
a=[Link]([Link]());
b=[Link]([Link]());
if(a<b){
profit=b-a;
[Link]("It’s profit of " +profit);
}
else
{
loss=a-b;
[Link]("It’s loss of " +loss);

4
}
}}
Output:
Enter the C.P and S.P :
1000
800
It’s loss of 200

Q2 W.A.P to take a number as input from


the user and check that number is even or
odd.
Program:-
import [Link];
class main
{
public static void main(String[] args)

5
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number:");
int num = [Link]();
if(num % 2 == 0)
{
[Link]("Number " +num+ " is
even");
}
else
{
[Link]("Number " +num+" is
odd");
}
}
}

6
Output:
Enter a number:
28
Number 28 is even

Q3 WAP to accept the age and find whether it is


eligible to vote or not.
[Link].*;
class program{
public static void main(String args[]) throws
IOException
{
BufferedReader in=new BufferedReader(new
InputStreamReader([Link]));
int age,
[Link](“enter an age”);
age =[Link]([Link]());

7
if (age>=18)
{
[Link](“eligible to vote”);
}
else
{
[Link](“not eligible to vote”);
}}}
Output-
Enter an age-
23
Eligible to vote
Q4 WAP to check whether the number is
divisible by 5 or not.
Program:-
[Link].*;
class program

8
{
public static void main (String args[])throws
IOException
{
BufferedReader in=new BufferedReader(new
InputStreamReader([Link]));
int n;
[Link](“enter a number”);
n =[Link]([Link]());
if (n%5==0)
{
[Link](“Divisible”);
}
else
{
[Link](“Not divisible”);
}}}

9
Give output-
Enter a number
625
Divisible

Q5 W.A.P to check whether a number is


divisible with another number or not. Print
appropriate message.
Program:-
import [Link];
class main
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number that you
want to divide:");
int n1 = [Link]();
10
[Link]("Enter the divisor:");
int n2 = [Link]();
if(n1 % n2 == 0) {
[Link](n1+ " is divisible by " +n2);
}
else {
[Link](n1+ " is not divisible by "
+n2);
}}}
Output:
Enter the number that you want to divide:
10
Enter the divisor:
5
10 is divisible by 5
Q6 WAP to enter the sides of triangle and tell
whether the triangle is possible to make or not.

11
Program:-
import [Link].*;
class main
{
public static void main(String[]args)
{
Scanner sc = new Scanner([Link]);
[Link](“Enter the sides of triangle”);
int a = [Link]();
int b = [Link]();
int c = [Link]();
if((a+b>c)&&(a+c>b)&&(b+c>a))
{
[Link](“Its possible”);
}
else
{

12
[Link](“Its not possible”);
}
}
}

Output:-
Enter the sides of triangle
5
3
7
Its possible

Q7 WAP to enter a year and find it is a leap year


or not.

13
import [Link].*;
class program{
public static void main(String args[])throws
IOException{
Scanner sc=new Scanner([Link]));
[Link](“Enter a year”);
int year=[Link]();
if (year%4==0){
[Link](“Leap year”);
}
else{
[Link](“Not a leap year”);
}}}
Output:-
Enter year
2020
Leap year

14
Q8 Wap to accept 3 numbers and find the
greatest number.
Program:-
[Link].*;
class program
{
public static void main(String args[])throws
IOException
{
BufferedReader in=new BufferedReader(new
InputStreamReader([Link]));
int a,b,c;
[Link](“Enter three numbers”);
a =[Link]([Link]());
b =[Link]([Link]());
c =[Link]([Link]());
if (a>b&&a>c)
{
15
[Link](a+“is the greatest number”);
}
else if (b>a&&b>c)
{
[Link](b+" is the greatest number”);
}
else
{
[Link](c+“is the greatest number”);
}
}
}
Give output-
Enter three number
18
20
54

16
54 is the greatest number
Q9 W.A.P to create a printing application
program where we will take the number of
copies to be printed as input from the user and
then prints the price per copy and the total price
for the printing copies.
The chart price to print the number of copies is
given below:
0 – 99: $0.30 per
copy
100 – 499 $0.28 per
copy
500 – 799 $0.27 per
copy
800 – $0.26 per Program:-
1000 copy
Over $0.25 per import [Link];
1000 copy
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

17
[Link]("Enter the number of copies
to be printed:");
int noOfCopies = [Link]();
if(noOfCopies > 0 && noOfCopies < 100){
double pricePerCopies = 0.30;
[Link]("Price per copy: "+"$"
+pricePerCopies);
double totalCost = noOfCopies *
pricePerCopies;
[Link]("Total cost is "+"$"
+totalCost);
}
else if(noOfCopies >= 100 && noOfCopies <
500){
double pricePerCopies = 0.28;
[Link]("Price per copy: "+"$"
+pricePerCopies);

18
double totalCost = noOfCopies *
pricePerCopies;
[Link]("Total cost is "+"$"
+totalCost);
}
else if(noOfCopies >= 500 && noOfCopies <
800){
double pricePerCopies = 0.27;
[Link]("Price per copy: "
+"$"+pricePerCopies);
double totalCost = noOfCopies *
pricePerCopies;
[Link]("Total cost is "+"$"
+totalCost); }
else if(noOfCopies >= 800 && noOfCopies <
1000){
double pricePerCopies = 0.26;
[Link]("Price per copy: "+"$"
+pricePerCopies);

19
double totalCost = noOfCopies *
pricePerCopies;
[Link]("Total cost is "+"$"
+totalCost);
}
else {
double pricePerCopies = 0.25;
[Link]("Price per copy: "+"$"
+pricePerCopies);
double totalCost = noOfCopies *
pricePerCopies;
[Link]("Total cost is "
+"$"+totalCost);
}}}
Output:
Enter the number of copies to be printed:
1151
Price per copy: $0.25

20
Total cost is $287.75
Q10 WAP to check whether a number is
positive, negative or zero.
import [Link].*;
class program
{
public static void main(String args[])throws
IOException
{
BufferedReader in=new
BufferedReader(new
InputStreamReader([Link]));
int n;
[Link](“Enter a number”);
n =[Link]([Link]());
if (n>0)
{

21
[Link](“Positive number”);
}
else if(n<0)
{
[Link](“Negative number”);
}
else
{
[Link](“Zero”);
}
}
}

Give output-
Enter a number-
46

22
Positive number. 
Q11 Write a program to input 3 unique
integers and print the smallest among them.
Program:
import [Link].*;
class main
{
public static void main(String[]args)
{
Scanner sc=new Scanner([Link]);
int a,b,c;
[Link](“Enter 3 integers:”);
a=[Link]();
b=[Link]();
c=[Link]();
if(a<b && a<c)

23
[Link](“Smallest:”+a);
else if(b<a && b<c)
[Link](“Smallest:”+b);
else
[Link](“Smallest:”+c);
}
}

Output:-
Enter 3 integers:
4
25
1
Smallest:1

24
Q12 Write a program to accept three sides of a
triangle as parameter and check whether it can
form a triangle or not. If it forms a triangle,
check whether it is an acute angled, obtuse
angled
or right-angled triangle.
(Hint: To form a triangle, each side should be
less the sum of the other two sides.
To form an acute angled triangle the square of
every side should be less than the sum of the
squares of the other two sides.
To form an obtuse angled triangle the square of
any side should be greater than the sum of the
squares of the other two sides.
To form an right angled triangle the square of
any side should be equal to the sum of the
squares of the other two sides.)

25
Program:
import [Link].*;
class main
{
public static void main(String[]args)
{
Scanner sc=new Scanner([Link]);
int a,b,c;
[Link](“Enter 3 sides:”);
a=[Link]();
b=[Link]();
c=[Link]();
if(a<b+c && b<a+c && c<a+b)
{
if(a*a<b*b+c*c && b*b<a*a+c*c &&
c*c<a*a+b*b)

26
{
[Link](“Acute angled triangle”);
}
else if(a*a>b*b+c*c || b*b>a*a+c*c ||
c*c>a*a+b*b){
[Link](“Obtuse angled triangle”);
}
else{
[Link](“Right angled triangle”);
}}
else{
[Link](“Cannot form a triangle”);
}}}
Output:
Enter 3 sides:
3
5

27
7
Obtuse angled triangle

Q13 Write a program to accept a mark obtained


by a student in computer science and print the
grades accordingly:
Marks Grade
Above 90 A
70 to 89 B
50 to 69 C
below 50 D

import [Link].*;
class main
{
public static void main(String[]args){

28
Scanner sc=new Scanner([Link]);
int c;
[Link](“Enter marks in Computer
science:”);
c=[Link]();
if(c>=90)
[Link](“Grade=A”);
else if(c>=70 && c<90)
[Link](“Grade=B”);
else if(c>=50 && c<70)
[Link](“Grade=C”);
else
[Link](“Grade=D”);
}
}

Output:-

29
Enter marks in Computer science:
88
Grade=B

Q14 A cloth showroom has announced the


following festival discounts on the purchase of
items,
based on the total cost of the items purchased:
Total Cost Discount (in Percentage)
Less than 2000 5%
2001 to 5000 25%
5001 to 10000 35%
Above 10000 50%
Write a program to input the total cost and
compute and display the amount to be paid by
the
customer after availing the discount.
import [Link].*;
class main4

30
{
public static void main(String[]args){
Scanner sc=new Scanner([Link]);
float tc,d,ap;
[Link]("Enter the total cost of the
items:");
tc=[Link]();
if(tc<=2000)
d=5/100f*tc;
else if(tc>=2001 && tc<=5000)
d=25/100f*tc;
else if(tc>=5001 && tc<=10000)
d=35/100f*tc;
else
d=50/100f*tc;
ap=tc-d;
[Link]("Amount Payable:"+ap);

31
}
}
Output:-
Enter the total cost of the items:
5456
Amount Payable:3546.4
Q15 Write a program to find the sum of all 3
digit odd natural numbers, which are multiples
of 5.
class ans
{
public static void main(String[]args)
{
int i,s=0;
for(i=101;i<=999;i+=2)
{
if(i%5==0)

32
s+=i;
}
[Link](“Sum=”+s);
}
}
Output:-
Sum=49500
Q16 Write a program to input an integer and
find its factorial.
import [Link].*;
class ans1{
public static void main(String[]args){
int i,n,f=1;
Scanner sc=new Scanner([Link]);
[Link](“Enter an integer:”);
n=[Link]();
for(i=1;i<=n;i++){

33
f=f*i;
}
[Link](“Factotrial:”+f);
}}
Output:-
Enter an integer:
5
Factotrial:120
Q17 Write programs for each of the following to
print the series: 1, 2, 4, 7, 11, 16, 22, 29, …,
upto n terms.
import [Link].*;
class Sol17{
public static void main(String[]args){
int i,n,s=1;
Scanner sc= new Scanner([Link]);
[Link]("Enter the number of
terms:");
34
n=[Link]();
for(i=1;i<=n;i++){
[Link](s+" ");
s=s+i;
}}}
Output:-
Enter the number of terms:
15
1 2 4 7 11 16 22 29 37 46 56 67 79 92 106
Q18 Write programs for each of the following to
print the series: 2, 4, 8, 14, 22, 32, 44, 58, …,
upto n terms
import [Link].*;
class Sol{
public static void main(String[]args){
int i,n,s=2,c=2;
Scanner sc=new Scanner([Link]);

35
[Link]("Enter the number of terms:");
n=[Link]();
for(i=1;i<=n;i++){
[Link](s+" ");
s=s+c;
c=c+2;
}}}
Output:-
Enter the number of terms:15
2 4 8 14 22 32 44 58 74 92 112 134 158 184 212
Q19 Write programs for each of the following to
print the series: 1, 2, 5, 10, 17, 26, 37, 50, …,
upto n terms.
import [Link].*;
class Sol{
public static void main(String[]args){
int i,n,s=1,c=1;

36
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of terms:");
n=[Link]();
for(i=1;i<=n;i++){
[Link](s+" ");
s=s+c;
c=c+2;
}}}
Output:-
Enter the number of terms:15
1 2 5 10 17 26 37 50 65 82 101 122 145 170 197
Q20 WAP to print the series: 1, 1, 2, 3, 5, 8, 13,
…, upto n terms.
import [Link].*;
class Sol20{
public static void main(String[]args){
int i,n,f=1,s=0,t;

37
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of terms:");
n=[Link]();
for(i=1;i<=n;i++){
t=f+s;
[Link](t+" ");
f=s;
s=t;
}}}
Output:-
Enter the number of terms:15
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Q21 W.A.P to add the number from 1 to 10
using while loop and display the sum on the
console.
Program:-
public class Test {

38
public static void main(String[] args)
{
int sum = 0, i = 1;
while (i <= 10) {
sum = sum + i;
i++;
}
[Link]("sum is " + sum);
}
}
Output:-sum is 55

Q22 W.A.P to calculate the mid-value between i


and j where i is equal to 10 and y is equal to 20.
Program:-
public class Test {

39
public static void main(String[] args)
{
int x = 10, y = 20;
while (++x < --y); // No body in this loop.
[Link]("Mid value is " + x);
}
}
Output:
Mid value is 15

Q23 Wap to print from 10 to 1 using while loop.


Program
class WhileLoopMain {
public static void main(String[] args) {
int i=10;
while(i>0)
{

40
[Link](i);
i--;
}}}
Output:-10
9
8
7
6
5
4
3
2
1
Q24 Wap a program where we will display
numbers from 1 to 6.
Program:-
public class Test {

41
public static void main(String[] args)
{
int x;
x = 1; // starting number is 1.
while(x <= 6)
{
[Link](x+“ “); // print x value.
x++; // increment x value by 1.
} // This statement will be executed as long as
x <= 6.
}
}
Output:1 2 3 4 5 6

Q25 Wap to accept numbers and print their sum


using do while loop.
Program:-

42
import [Link];
public class Test
{
public static void main(String[] args)
{
int num = 0, sum = 0;
Scanner sc = new Scanner([Link]);
while(num != 0)
{
// Read the next input.
[Link]("Enter an integer
number");
num = [Link]();
sum = sum + num;
}
[Link]("Sum of numbers: " +sum);
}

43
}
Output:
Enter an integer number
20
Enter an integer number
10
Enter an integer number
5
Enter an integer number
40
Enter an integer number
0
Sum of numbers: 75

44
Bibliography
In this project I have taken help from my
course book
ICSE UNDERSTANDING COMPUTER
APPLICATIOS WITH BLUEJ CLASS
IX written by Vijay kumar pandey and
Dilip kumar dey and published by Arya
Publishing Company

45

Common questions

Powered by AI

Decision-making processes in Java, primarily relying on if-else constructs, directly impact the accuracy and reliability of computational results by controlling the flow of execution according to specific conditions. Misconfigured logic leads to incorrect computations, as seen when complex conditions such as profit/loss computations or validation checks are involved. Well-structured conditions ensure correct results, while flawed logic could lead to misinterpretations of user input or condition checks, affecting reliability .

Input/output in Java applications enhances interactivity by allowing dynamic interaction between the user and the program. Through classes like Scanner or BufferedReader, users can provide inputs that the program processes to produce tailored outputs. This capability turns static scripts into dynamic applications capable of responding to user choices and delivering real-time feedback, thus enhancing user engagement and application versatility .

Testing and debugging Java applications with conditional logic require a comprehensive approach that includes unit tests to cover each logical branch, ensuring conditions are correctly evaluated and expected outcomes are met. Incorporating print statements or using debugging tools helps track variable states and logical flow real-time. Identifying edge cases, such as minimum or maximum input values, ensures robustness. Testing iteratively corrects any logical discrepancies identified, thereby affirming the application’s accuracy and reliability .

Java handles nested conditions using nested if-else statements, allowing more granular control over program flow based on multiple criteria. For instance, in determining a triangle type, conditions are nested to first verify if a triangle is possible, then check if it’s acute, right, or obtuse. Nested conditions provide a hierarchical logic structure, enable complex decision-making, and reduce code duplication by grouping related checks systematically .

When structuring a program to check conditions for geometric figures like triangles, several considerations must be taken into account: ensuring that the sum of the lengths of any two sides is greater than the third side (valid triangle condition), and then further checking for angles using squares of sides to determine if the triangle is acute, right, or obtuse. These conditional checks demand careful logical sequencing and thorough validation to ensure correctness and robustness of the program logic .

Programmatic sequences in Java effectively utilize loops to manage and execute repetitive calculations. Loops, like the for loop, iterate over a range and apply consistent operations, such as summing series or generating sequences, as shown in multiple examples. The efficient looping construct avoids redundancy, reduces complexity, and improves code maintainability by consolidating similar computations under a single logic block executed multiple times .

Input validation in Java is crucial in ensuring that user inputs match expected types and formats, preventing errors and potential security risks. Programs using BufferedReader or Scanner to read inputs often need validation checks, like ensuring non-zero denominators in division or positive integers for factorial calculations. Without such checks, programs might crash or produce incorrect results. Hence, validating inputs before processing is a standard practice .

Java programs use the modulo (%) operation in conditional statements to determine numeric properties, like divisibility and parity. For example, an if condition checking 'num % 2 == 0' determines if a number is even, while 'n % 5 == 0' in another program checks divisibility by 5. These operations help in creating concise and efficient logic for validating conditions involving multiples or specific numeric properties in a program .

Loops such as for loops and while loops are significant in Java for repetitive tasks. The for loop is utilized for iterating over a range with known bounds, great for executing a fixed number of iterations (e.g., printing series or summing numbers). In contrast, while loops continue until a certain condition is met, useful when the number of iterations is not known ahead (e.g., accepting numbers until a sum is displayed). Both are critical for reducing redundancy and implementing efficient logic .

The Java program structure uses if-else statements to execute different blocks of code based on conditions. In the provided examples, if statements are used to check conditions such as whether a number is even or odd, or if a cost price is less than the selling price to determine profit. The if-else construct allows executing a specific set of statements if the condition is true and another set if false, enabling decision-making within the code .

You might also like