0% found this document useful (0 votes)
7 views3 pages

Java Fundamentals Holiday Homework

Java Practice sheet for bigneerrs
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)
7 views3 pages

Java Fundamentals Holiday Homework

Java Practice sheet for bigneerrs
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

CITY MONTESSORI SCHOOL

Gomti Nagar - I
CT & ICT
CLASS – 8
Subject – Fundamentals of Java
Holiday Homework

1. Case Study

Q: You are using a Calculator app. When you press '=' it gives you the result, but you don’t
know the internal calculations.

A: The OOP concept applied here is Encapsulation.


Why? Because the internal logic (data and methods) is hidden from the user. The calculator
performs the operation internally and only shows the final result.

2. Case Study

Q: A parent class Shape has a method draw(). The subclasses Circle, Rectangle, and Triangle
each provide their own version of draw().

A: The OOP concept used here is Polymorphism.


Explanation: Polymorphism allows methods to behave differently based on the object
calling them. Each subclass provides its own implementation of the draw() method.

3. Predict the Output

int a = 17, b = 3, result;


result = a % b;
[Link](result);

Answer: a) 2

4. Evaluate the following

If a = 12, b = 5
b = a / b + b % 3;
Answer: b = 4

5. Convert into Java Expressions:


A = b * b + 4 * a * c;

x = (a + b) * (a - b);

F = m * a;

E = 0.5 * m * v * v;

area = 3.14 * r * r;

6. Java Program to Print Name, Class, Roll No:

class PersonalInfo {
public static void main(String[] args) {
[Link]("Name: ");
[Link]("Vinayak\n");
[Link]("Class: ");
[Link]("8\n");
[Link]("Roll No: ");
[Link]("50");
}
}

7. Program to Calculate Speed:

class Speed {
public static void main(String[] args) {
int distance = 50;
int time = 1;
int speed = distance / time;
[Link]("Speed = " + speed + " km/hr");
}
}

8. Program to Calculate Simple Interest:

import [Link];
class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Principal: ");
double p = [Link]();
[Link]("Enter Rate of Interest: ");
double r = [Link]();
[Link]("Enter Time: ");
double t = [Link]();
double si = (p * r * t) / 100;
[Link]("Simple Interest = " + si);
}
}

9. Assign Java Data Types:

A = 45 → int A = 45;

B = 'A' → char B = 'A';

C = "Java" → String C = "Java";

D = 67.89 → double D = 67.89;

F = 23.567f → float F = 23.567f;

10. Debug the Program:

Incorrect:

class Division {
void calculate(double a, b) {
double result = a / b;
[Link]("Result is : " + result);
}
}

Correct:

class Division {
void calculate(double a, double b) {
double result = a / b;
[Link]("Result is : " + result);
}
}

Common questions

Powered by AI

Encapsulation enhances the functioning of a calculator application by hiding the internal computations—data and methods—from the user. This means that users can only access the final result of calculations after pressing '=', without knowing how these results are computed internally. Encapsulation is vital in Object-Oriented Programming as it provides data protection and reliability, ensuring that direct access to application details is controlled and modifications are restricted to specified methods .

In Java programming, it is essential to distinguish between integer and floating-point division because these operations yield different results. Integer division truncates the decimal portion, providing only the integer quotient, while floating-point division preserves the decimal part. This distinction impacts calculations significantly, as mixing them without careful consideration can lead to unintended results and logical errors in programs, particularly where precision is crucial .

The program snippet to calculate the area of a circle is: double radius = 5.0; double area = 3.14 * radius * radius; System.out.println("Area = " + area);. In this calculation, using 'double' for the 'radius' and 'area' variables is significant because it allows for accurate representation of decimal values common in geometric calculations. Choosing the correct data types helps maintain precision and avoids fractional truncation, providing accurate results which are essential in real-world applications .

Input validation in Java programs, such as the Simple Interest calculator, is crucial for ensuring that the input data is correct and relevant to the expected context. Without it, programs may accept invalid, unexpected, or malicious inputs, leading to incorrect computations, application crashes, or security vulnerabilities. By validating inputs, developers can ensure reliability, correctness, and security of the application output, reducing risk and improving user trust .

Encapsulation differs from other Object-Oriented Programming principles like inheritance and polymorphism as it focuses primarily on restricting and controlling access to the internal workings of an object. It is significant in maintaining system integrity by ensuring that an object's state can only be modified through its own methods, providing a protective layer against unintended interference and misuse of its internal elements. This control enhances the robustness and security of software by exposing only necessary interfaces for interaction .

Declaring variables with explicit data types in Java is necessary because Java is a statically-typed language, meaning that variable types are checked at compile-time. This practice ensures type safety, helps prevent errors, and makes the code more readable and maintainable. Not declaring data types can lead to compilation errors, unexpected results, and runtime exceptions, as the Java compiler cannot infer types for operations or assignments .

To convert the mathematical expression "E = 0.5 * m * v * v" into a Java statement, you would declare variables for 'm' and 'v' using appropriate data types, such as 'double' if dealing with decimal values. Then, compute the value of 'E' using these variables: double E = 0.5 * m * v * v;. Ensuring 'm' and 'v' have assigned values before computation is important for the correct execution of this statement .

Polymorphism is a concept in programming that allows methods to perform different tasks based on the object invoking them. In the context of a Shape class with subclasses like Circle, Rectangle, and Triangle, polymorphism manifests when each subclass provides its own version of the 'draw()' method. This allows the 'draw()' method to behave uniquely depending on the subclass instance calling it, demonstrating the flexible nature of polymorphic behavior .

In the original Java class snippet, the declaration of the method "calculate(double a, b)" is incorrect because each parameter must have its type explicitly specified. The correction involves specifying the data type for each parameter: "void calculate(double a, double b)". This explicitly defines both parameters 'a' and 'b' as doubles, resolving the syntactical error and allowing the method to execute without compilation issues .

An example program using the Java modulus operator is: int a = 17, b = 3, result; result = a % b; System.out.println(result);. In this expression, the modulus operator '%' computes the remainder of the division of 'a' by 'b'. Here, '17 % 3' equals '2' because 17 divided by 3 gives a quotient of 5 and a remainder of 2. The output of this program would be '2', reflecting the remainder of the division .

You might also like