0% found this document useful (0 votes)
2 views

Computer Assignment Term-3

This document is an assignment for Computer Applications at Hiranandani Foundation School, detailing various Java programming tasks and concepts. It includes questions on Java expressions, Math functions, program evaluations, explanations of programming terms, and coding tasks related to Dudeney numbers and parking charges. The assignment is structured with an index, questions, and answers, showcasing practical applications of Java programming.

Uploaded by

Piyush Sharma
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)
2 views

Computer Assignment Term-3

This document is an assignment for Computer Applications at Hiranandani Foundation School, detailing various Java programming tasks and concepts. It includes questions on Java expressions, Math functions, program evaluations, explanations of programming terms, and coding tasks related to Dudeney numbers and parking charges. The assignment is structured with an index, questions, and answers, showcasing practical applications of Java programming.

Uploaded by

Piyush Sharma
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
You are on page 1/ 19

HIRANANDANI FOUNDATION SCHOOL, THANE

COMPUTER APPLICATIONS
ASSIGNMENT

2022-23 TERM 3

Name: PRATHMAY SHARMA


Class/Div: IX A
Roll No: 32

1
INDEX
Q.No. Question Page Number
1 Write Java expression for the following. 3
2 Purpose and return data type of the given Math 4
functions
3 Evaluation of the given expressions 5,7
4 Give the output of the following statements 6
5 Explain the following terms 8
6 Program to check print whether a number is a 9,10,11
Dudeney number or not.
7 Program to input number of hours parked and 14,15
print the charges as per given criteria.
8 Program to print pattern and sum of series 16,17,18

Teacher’s Signature: _____________________________________

2
Question 1

Write Java expression for the following using Math function as required.
i) a^3 + b^3 + 3ab(a-b)
Ans: Math.pow(a,3)+Math.pow(b,3)+3*a*b*(a-b)

ii) ii) 5x^3+2y/(x+y)


Ans: Math.pow((5*x),3)+(2*y)/(x+y)

3
Question 2
State the purpose and return data type of the following functions.
i) Math.floor()
Ans: Return the lower interger value i:e < or = the argument
Return datatype:- double

ii) Math.random()

Ans: Returns the lower integer number between 0 and 1


Return datatype:- double

4
Question 3
Evaluate the following expressions.
i) c%= a/b + (++b + a--) - c++; when a=4, b=7 and c=3
Ans: c=c%(a/b+(++b+a--)-c++)
c=3%(4/7+(5+7)-3)
c=3%(4/7+12)-3)
c=3%(0+12)-3)
c=3%12-3
c=3-3
c=0

ii) m = m++ + m*n + --n - ++m; when m = 5 and n = 8


Ans: m=5+6*8+7-7
m=5+48+7-7
m=53+7-7
m=60-7
m=53

5
Question 4
Give the output of the following
i) nt i = -10;
do
{
if(i%2==0 && i>=0)
break;
else
System.out.println(i);
i+=2;
}
while(i<=10);

Ans: Output= -10


-8
-6
-4
-2

ii) System.out.println(Math.abs(Math.max(-5,-7)) +
Math.ceil(9.3)+Math.floor(-3.2));
Ans: Math.abs(-5)+(10.0)+(-4.0)
Math.abs(-5+10-4)
Math.abs(1)
1

6
int i,j;

for (i=0; i<4;i++)


{
for (j=I;j>=0;j--)
System.out.print(j);
System.out.println(); }
Ans: 1
0

1
0

2
1
0

3
2
1
0

7
Question 5
Explain the following
i) Encapsulation
Ans: Encapsulation in Java refers to integrating data (variables) and code
(methods) into a single unit.

ii) Literals
Ans: Literals in Java are a synthetic representation of boolean, character,
numeric, or string data.

iii) Features of Java


Ans: Simple
Object-Oriented
Portable
Platform independent
Secured
Robust

8
Question 6
Do the following conversions:
i) Convert the following if-else construct to switch case.
if(m==0){
x= x+2;
System.out.println("X=" +x);
}
else if(m==1){
x= x+4;
System.out.println("X=" +x);
}
else { x= x+6;
System.out.println("X=" +x);
}

Ans: Switch(x)
case:1
{
x= x+2;
System.out.println("X=" +x);
}
case:2
{
x= x+4;
System.out.println("X=" +x);

9
}
case:3
{
x= x+6;
System.out.println("X=" +x);
}
default:
{
System.out.println(“ERROR”);
}

ii) Convert the following ternary operator to if-else.


String result = (year % 4 == 0 && year % 100 != 0) ? "is a leap year" :
"is not a leap year";
Ans: if(year%4==0&&year%100!=0)
{System.out.println(“is a leap year”);}
else
{System.out.println(“is not a leap year”);}

iii) Convert the following while loop to a for loop.


while (number != 2) {
System.out.println(number);
number = 2;
System.out.println(number);
number = 3; }

10
Ans: for(;number!=2;) {
System.out.println(number);
number = 2;
System.out.println(number);
number = 3; }

11
Question 6
A Dudeney number is a positive integer that is a perfect cube such that the sum
of its digits is equal to the cube root of the number. Write a program to input a
number and check and print whether it is a Dudeney number or not.
Example:
Consider the number 512.
Sum of digits = 5 + 1 + 2 = 8
Cube root of 512 = 8
As Sum of digits = Cube root of Number hence 512 is a Dudeney number.
Ans: import java.util.Scanner;

public class KboatDudeneyNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = in.nextInt();

//Check if n is a perfect cube


int cubeRoot = (int)Math.round(Math.cbrt(n));
if (cubeRoot * cubeRoot * cubeRoot == n) {
//If n is perfect cube then find
//sum of its digits
int s = 0;
int t = n;
while (t != 0) {

12
int d = t % 10;
s += d;
t /= 10;
}

if (s == cubeRoot) {
System.out.println(n + " is a Dudeney number");
}
else {
System.out.println(n + " is not a Dudeney number");
}
}
else {
System.out.println(n + " is not a Dudeney number");
}
}
}

Question 7
13
Indian Railway Stand decided to take charge for the parking of 2 wheeler / 3
wheeler / 4 wheeler as per the following criteria:
No. of Hrs. Parking Charges
upto 8 hrs Rs 10
next 8 hrs Rs. 6
above 16 hours Rs. 5 for each additional 8 hr.
Input number of hours parked and print the charges as per given criteria.
Ans:
import java.util.Scanner
public class abc
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println(“Enter the no of hours for parking”);
int n=sc.nextInt();
int c=0;
if(n>0 && n<=8)
{
c=10;
System.out.println(c);
}
else if(n>8 && n<=16)
{
c=10+6;
else if(n>16)

14
{
int x=(n-16)/8;
int y=(n-16)%8;
if(y>0)
{
c=10+6+x*5+5;
System.out.println(c);
}
else
{
c=10+6+x*5;

System.out.println(c);
}
}
}
}

Question 8

15
Write a program for each of the following.
1)
1
26
3 7 10
4 8 11 13
5 9 12 14 15
Ans:
public class abc2
{
public static void main()
{
int k=0; int p=4:
for(int i=1;i<=5;i++)
{
k=i; p=4;
for(int j=1;j<=i;j++)
System.out.print(k+ “”);
k=k+p; p--;
}
System.out.println();
}

2) S = 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + ……… to n terms

16
Ans:

import java.io.*;
class MathSeries {
// Function to get the series
static double Series(double x, int n)
{
double sum = 1, term = 1, fct, j, y = 2, m;

// Sum of n-1 terms starting from 2nd term


int i;
for (i = 1; i < n; i++) {
fct = 1;
for (j = 1; j <= y; j++) {
fct = fct * j;
}
term = term * (-1);
m = Math.pow(x, y) / fct;
m = m * term;
sum = sum + m;
y += 2;
}
return sum;
}

17
public static void main(String[] args)
{
double x = 3;
int n = 4;
System.out.println(Math.round(Series(x, n) *
10000.0) / 10000.0);
}
}

********************************************************************************

18
19

You might also like