0% found this document useful (0 votes)
21 views16 pages

Java Programs for Class 9 Students

The document contains 24 programming questions and their solutions in Java. Each question asks the user to input certain values, performs some calculations based on the inputs, and outputs the result. The questions cover basic concepts like input/output, arithmetic operations, conditional statements, loops etc. The solutions provide the full Java code to implement the program for each question.
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)
21 views16 pages

Java Programs for Class 9 Students

The document contains 24 programming questions and their solutions in Java. Each question asks the user to input certain values, performs some calculations based on the inputs, and outputs the result. The questions cover basic concepts like input/output, arithmetic operations, conditional statements, loops etc. The solutions provide the full Java code to implement the program for each question.
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

Rasraj Bodake

Class:9C
Roll no. 19
Input in Java
SECTION B
Programs:
1.
Ans.
class Question1
{
void main(int a, int b)
{
int c,d;
c=a+b;
d=a*b;
[Link](“Sum=”+c);
[Link](“Product=”+d);
}
}
2.
Ans.
import [Link].*;
class Question2
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c,d,e;
float av;
[Link](“Enter 5 integers:”);
a=[Link]();
b=[Link]();
c=[Link]();
d=[Link]();
e=[Link]();
av=(float)(a+b+c+d+e)/5;
[Link](“Average=”+av);
}
}
3.
Ans.
import [Link].*;
class Question3
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c,d,e;
[Link](“Enter 2 integers:”);
a=[Link]();
b=[Link]();
c=a+b;
d=a-b;
e=c*d;
[Link](“Product=”+e);
}
}
4.
Ans.
import [Link].*;
class Question4
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c,s;
float av,d;
[Link](“Enter 3 integers:”);
a=[Link]();
b=[Link]();
c=[Link]();
s=a+b+c;
av=s/3f;
d=s-av;
[Link](“Difference=”+d);
}
}
5.
Ans.
import [Link].*;
class Question5
{
static void main()
{
Scanner sc=new Scanner([Link]);
float p,r,t,si;
[Link](“Enter the principal:”);
p=[Link]();
[Link](“Enter the rate:”);
r=[Link]();
[Link](“Enter the time:”);
t=[Link]();
si=(p*r*t)/100;
[Link](“Simple Interest=”+si);
}
}
6.
Ans.
import [Link].*;
class Question6
{
static void main()
{
Scanner sc=new Scanner([Link]);
float l,b,a,p;
[Link](“Enter the length:”);
l=[Link]();
[Link](“Enter the breadth:”);
b=[Link]();
a=l*b;
p=2*(l+b);
[Link](“Area=”+a);
[Link](“Perimeter=”+p);
}
}
7.
Ans.
import [Link].*;
class Question7
{
static void main()
{
Scanner sc=new Scanner([Link]);
float r,a,pi=3.142f,c;
[Link](“Enter the radius:”);
r=[Link]();
a=pi*r*r;
c=2*pi*r;
[Link](“Area=”+a);
[Link](“Circumference=”+c);
}
}
8.
Ans.
import [Link].*;
class Question8
{
static void main()
{
Scanner sc=new Scanner([Link]);
float l,b,h,v,a;
[Link](“Enter the length:”);
l=[Link]();
[Link](“Enter the breadth:”);
b=[Link]();
[Link](“Enter the height:”);
h=[Link]();
v=l*b*h;
a=2*(l*b+b*h+h*l);
[Link](“Volume=”+v);
[Link](“Area=”+a);
}
}
9.
Ans.
import [Link].*;
class Question9
{
static void main()
{
Scanner sc=new Scanner([Link]);
float r,h,v,a,pi=3.142f;
[Link]( “Enter the radius:”);
r=[Link]();
[Link]( “Enter the height:”);
h=[Link]();
v=pi*r*r*h;
a=2*pi*r*r+2*pi*r*h;
[Link]( “ Volume=”+v);
[Link]( “Area=”+a);
}
}
10.
Ans.
import [Link].*;
class Question10
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c,s;
[Link](“Enter the 3 integers:”);
a=[Link]();
b=[Link]();
c=[Link]();
s=a%10+b%10+c%10;
[Link]( “Sum of the last digits=”+s);
}
}
11.
Ans.
import [Link].*;
class Question11
{
static void main()
{
Scanner sc=new Scanner([Link]);
float f,c;
[Link]( “Enter temperature in farenheit:”);
f=[Link]();
c=((f-32)*5)/9;
[Link](“Temperature in celcius=”+c);
}
}
12.
Ans.
import [Link].*;
class Question12
{
static void main()
{
Scanner sc=new Scanner([Link]);
int sec,h,m,s;
[Link]( “Enter time in seconds:”);
sec=[Link]();
h=sec/3600; //finding hours
sec=sec%3600; //remaining seconds
m=sec/60; //finding minutes
s=sec%60; //remaining seconds
[Link]( “Time in hours=”+h);
[Link]( “ Time in minutes=”+m);
[Link]( “ Time in seconds=”+s);
}
}
13.
Ans.
import [Link].*;
class Question13
{
static void main()
{
Scanner sc=new Scanner([Link]);
float n;
int r;
[Link]( “ Enter a floating point number:”);
n=[Link]();
r=(int)(n+0.5f);
[Link](“Answer=”+r);
}
}
14.
Ans.
import [Link].*;
class Question14
{
static void main()
{
Scanner sc=new Scanner([Link]);
float n,t,r;
[Link]( “Enter a floating point number:”);
n=[Link]();
t=(int)(n*100+0.5f);
r=t/100;
[Link]( “Answer=”+r);
}
}
15.
Ans.
import [Link].*;
class Question15
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c;
[Link]( “Enter 2 integers:”);
a=[Link]();
b=[Link]();
[Link](“Before Interchange:a=”+a+“and b=”+b);
c=a;
a=b;
b=c;
[Link](“After Interchange:a=”+a+“and b=”+b);
}
}
16.
Ans.
import [Link].*;
class Question16
{
static void main()
{
Scanner sc=new Scanner([Link]);
int h,m,s,sec;
[Link]( “Enter time in hours:”);
h=[Link]();
[Link]( “Enter time in minutes:”);
m=[Link]();
[Link]( “Enter time in seconds:”);
s=[Link]();
sec=h*3600+m*60+s;
[Link]( “Time in seconds:”+sec);
}
}
17.
Ans.
import [Link].*;
class Question17
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c,s;
[Link]( “Enter 3 integers:”);
a=[Link]();
b=[Link]();
c=[Link]();
s=a-(-b)-(-c);
[Link]( “ Sum:”+s);
}
}
18.
Ans.
import [Link].*;
class Question18
{
static void main()
{
Scanner sc=new Scanner([Link]);
float p,s,a;
[Link](“Enter the perimeter of a square:”);
p=[Link]();
s=p/4; //finding the side
a=s*s;
[Link](“Area of the square is:”+a);
}
}
19.
Ans.
import [Link].*;
class Question19
{
static void main()
{
Scanner sc=new Scanner([Link]);
float l,b,a,p;
[Link](“Enter the length of the rectangle:”);
l=[Link]();
[Link](“Enter the area of the rectangle:”);
a=[Link]();
b=a/l; //finding the breadth
p=2*(l+b);
[Link](“Perimeter of the rectangle:”+p);
}
}
20.
Ans.
import [Link].*;
class Question20
{
static void main()
{
Scanner sc=new Scanner([Link]);
float bp,da,hra,pf,np,gp;
[Link](“Enter the basic pay:”);
bp=[Link]();
da=25/100f*bp;
hra=15/100f*bp;
pf=8.33f/100*bp;
np=bp+da+hra;
gp=np-pf;
[Link](“Gross Pay:”+gp);
}
}
21.
Ans.
import [Link].*;
class Question21
{
static void main()
{
Scanner sc=new Scanner([Link]);
float l,b,p,s,a;
[Link](“Enter the length and breadth of the rectangle:”);
l=[Link]();
b=[Link]();
p=2*(l+b); //finding the perimeter
s=p/4; //finding the side of the square
a=s*s;
[Link](“Area:”+a);
}
}
22.
Ans.
import [Link].*;
class Question22
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,s;
[Link](“Enter an integer:”);
n=[Link]();
s=-n;;
[Link](“Sign changed:”+s);
}
}
23.
Ans.
import [Link].*;
class Question23
{
static void main()
{
Scanner sc=new Scanner([Link]);
float m1,m2,m3,m4,m5,agg,per;
[Link](“Enter the marks:”);
m1=[Link]();
m2=[Link]();
m3=[Link]();
m4=[Link]();
m5=[Link]();
agg=m1+m2+m3+m4+m5;
per=agg/500*100;
[Link](“Aggregate:”+agg);
[Link](“Percentage:”+per);
}
}
24.
Ans.
import [Link].*;
class Question24
{
static void main()
{
Scanner sc=new Scanner([Link]);
float tsp,tp,tcp,cp;
[Link](“Enter the total selling price:”);
tsp=[Link]();
[Link](“Enter the total profit:”);
tp=[Link]();
tcp=tsp-tp; //total cost price
cp=tcp/15; //cost price of one item
[Link](“Cost Price of one item:”+cp);
}
}

Common questions

Powered by AI

The document includes programs that handle unit conversions, illustrating conversions like Fahrenheit to Celsius using the formula ((f-32)*5)/9 and time conversion from seconds to hours, minutes, and remaining seconds. These examples showcase converting data types and recomputing numeric values to reflect equivalent units or scales, reinforcing the concept of scaling and measurement conversion in Java programming .

Using fixed constants like pi (3.142) in geometric calculations simplifies the coding process by ensuring consistency and readability. However, it may limit precision and accuracy in applications requiring more significant digits, such as scientific computations. Implementing constants directly also necessitates manual updates if precision requirements change, which could lead to procedural errors if not rigorously managed .

The Java programs calculate geometric properties by using formulas relevant to each shape. For instance, a rectangle’s area is computed using the formula length × breadth, and its perimeter with 2 × (length + breadth). Similarly, the document features programs calculating the area and circumference of a circle using pi × radius² and 2 × pi × radius, respectively. This illustrates a conceptual understanding of applying mathematical formulas to derive correct geometric measurements .

The programs take user input through the Scanner class, which prescribes specific methods for different data types, such as nextInt() for integers and nextFloat() for floating-point numbers. While the document doesn't explicitly address error handling, the use of defined methods provides a basic level of type safety by prompting the user to enter expected data, thus preventing some runtime errors. However, more robust error handling would require additional logic to catch exceptions and validate data types dynamically .

Programs in the document illustrate fundamental computational techniques like arithmetic operations, input handling, and basic control constructs. They can be adapted into exercises for introductory courses to teach students critical programming concepts, including input/output operations, data types, and simple algorithm implementations. Additionally, these programs help reinforce the connection between programming logic and real-world problem-solving by modeling relatable tasks like financial calculations and geometric measurements .

The Java programs demonstrate mathematical algorithms through computation of averages, geometric area and perimeter, volume calculations, etc., which directly parallel real-world problem-solving scenarios encountered in fields like physics, engineering, and finance. For instance, calculating simple interest or determining the area of physical objects translates directly to financial calculations or engineering designs, showcasing how computational algorithms model and solve practical problems .

The Java programs demonstrate basic logical operations and control structures primarily via input/output processing and variable manipulation. They illustrate logical constructs such as sequence (performing operations in order) and basic condition-based processing. For example, variable swaps and arithmetic operations that produce outputs based on different inputs allow learners to see how logical decisions translate into programmed actions, solidifying understanding of fundamental programming logic .

The document's Java programs utilize various mathematical operations such as addition, subtraction, multiplication, division, and modulus to perform calculations like sum, average, product, and simple interest. For example, programs are designed to calculate the sum and product of numbers, determine average values, and transform measurements using these operations. These functions enhance computation by providing fundamental arithmetic capabilities essential for solving a broad range of programming problems efficiently .

The programs model financial computations like simple interest and salary calculations by using straightforward arithmetic operations. Simple interest is computed with (p*r*t)/100 using user-specified inputs for principal, rate, and time. Salary computations incorporate basic pay adjustments for allowances and deductions. These models assume linear relationships and predefined formula usage, ensuring accuracy under normal conditions, though lacking complex financial variable considerations such as compounding or inflation .

The program structure influences readability and maintainability by utilizing consistent naming conventions and a simple organization of code blocks. Each Java class uses a clear, logical flow: declaring variables, obtaining user input, performing calculations, and displaying results. This streamlined approach aids comprehension and future code modifications. However, the lack of comments or detailed documentation might reduce maintainability over time, especially in complex scenarios where logic is difficult to trace without additional context .

You might also like