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

Java Programming Assignment Guide

The document outlines the instructions for completing Assignment_01 for Java Programming, which includes creating a folder, coding questions, and submission guidelines. Students are required to implement various programming tasks involving classes, objects, inheritance, and interfaces, along with deadlines for submission. Additionally, students must create a video presentation of their assignment and submit it via a provided link.

Uploaded by

utsavnautiyal2
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)
23 views6 pages

Java Programming Assignment Guide

The document outlines the instructions for completing Assignment_01 for Java Programming, which includes creating a folder, coding questions, and submission guidelines. Students are required to implement various programming tasks involving classes, objects, inheritance, and interfaces, along with deadlines for submission. Additionally, students must create a video presentation of their assignment and submit it via a provided link.

Uploaded by

utsavnautiyal2
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

Assignment_01

Unit_01 && Unit_02


Java Programming [TCS-408]

Instructions:
1- Create a folder name Assignment_01 in the same workspace where
you have created Unit_01 and Unit_02. Kindly follow the image.

2- Create Q1, Q2, Q3 and so on for all coding related questions in this
assignment

3- Deadlines and Links:


- You guys are supposed to complete this assignment by
11PM 28th April 2022 over GitHub and the video by 5PM
8th May-2022.
- Use the given link to provide the GitHub link for this
assignment. Choose Assignment_01 from the list.
- Link:
[Link]
4- Post exam you guys have to submit a small video of this
assignment in given format and order:
a. Use Project or Thread file [A4 Size]
b. Take printout of all the codes with their terminal output
from Assignment_01 folder you have created in the
same work space
c. All the pages would be of A4 size
d. First page would be your information page
e. After first page put the print out of this Assignment_01
[Take printout of this pdf]
f. Now at last, Combine all the answers [Handwritten and
Printouts] according to the order of the questions.

5- Now create a small video of this Assignment_01 from phone


and flip the pages of the file slowly.

6- Do upload this small video [30 seconds expected], over your


G-drive

7- Now create a public link of this video [use share with


everyone with this link option]

8- Finally, use this link to upload the link of your video.


[Link]
Questions:
Q1- WAP which would contain 6 objects, of a class
Student. Student [Name, Age, section, percentage]. They all belong to
Section-A. Your program would be able to find the average percentage of
students in this section. Use constructors to create these 6 objects and
input from Scanner class.

Q2- WAP to count the total number of calls for a member function from
more than one objects. [Let’s say, from 3 such Objects]

Q3- Given an interface in1 which includes a method display which takes
an integer as input .
interface In1
{
void display(int p);
}
Task is to write a class testClass which implements interface In1 and has
a method named display which takes an integer as an input p and the
display method should be able to tell if the number is prime or not?
- The main method should not be in testClass, create a separate class for
that.

Q4. Create a multi-level inheritance hierarchy from ClassOne, ClassTwo,


ClassThree classes.

ClassOne contains one, single parameterized constructor


ClassTwo contains only a default constructor
ClassThree contains one, single parameterized constructor and a default
constructor.

Create an object of top child class and make sure the constructor
execution will take place according to respective multi-level inheritance
hierarchy.

ClassThree obj = new ClassThree(1);


ClassThree obj = new ClassThree();
5Q. WAP in Java which would contain 8 objects of a Class Employee.
Employee contains name, age, department, salary. Your program would
be able to calculate the total salary to be paid in each department. Use
constructors to create these 8 objects and Scanner to take inputs.

Restrictions:
1- Minimum 8 objects [can be created manually with parameterized
constructors with Scanner input]
2- Departments are, A, B, C, D: minimum 4 departments [String Type]
3- If the Salary exceeds more than 30,000, then the default salary
would be 25000 of any of the employee
4- Use array of objects, instead of using multiple 8 objects separately
Use:
Employee arr[] = new Employee[8];

And

arr[i].[Link]("A");

Q6. As given there are two sample methods: First one returns the sum of
two numbers a and b and second one returns the subtraction of a and b. if
a-b is negative then return value would be 0. Use ternary operator to call
these two methods. If a>b then ternary should call sum(a,b) but if a<b
then ternary operator should call sub(a,b). Eventually print the result of
the ternary operation on variable a and b.
Note: Ternary operator can call a method even for true and false values

Q7. WAP to count the total number of calls for a member function:
display(), from more than one objects and how many times the object is
created of a class name Employee.
Note: Call display() function from more than 3 objects

Q8. WAP in java, to delete index=3, element from the given array
mentioned below.

Int[] arr = {1,2,3,4,5,6,7,8,9}


Note: Non relevance places would be filled with zeros.

Q9. WAP in java, which would demonstrate access protection over a


class and a variable of that class.
Note: class can be public and default while variable can be default,
private, protected, and public.
(If required then do create more than 2 packages)

Q10. WAP in Java Program To Survey Four Different Car Models For Four
Different Cities:

Your job is to find out the total number of cars sold of each model in all
the cities. Use Array of objects or multiple objects of class name City.

Q11. As given there are two sample methods: First one returns the sum of
two numbers a and b and second one returns the subtraction of a and b. if
a-b is negative then return value would be 0. Use ternary operator to call
these two methods. If a>b then ternary should call sum(a,b) but if a<b
then ternary operator should call sub(a,b). Eventually print the result of
the ternary operation on variable a and b.
Note: Ternary operator can call a method even for true and false values

Q12. If We Place Return Type In Constructor Prototype Will It Leads To


Error?

Q13. How Compiler And Jvm Can Differentiate Constructor And Method
Definitions Of Both Have Same Class Name?

Q14. Can we create a private constructor?

Q15. Can we have a Constructor in an Interface?

Q16. Describe various forms of implementing interfaces and extending


an Abstract Class. Explain it with suitable examples.
Note:
Interface:
1. Functional Interface [only on abstract method, like
runnable ]
2. Marker interface [0-any abstract methods]

Abstract Class:
1. Pure or 100% abstract class
2. Normal Abstract Class [0-100% abstract methods]

Q17. How a 100% abstract class is different from an Interface. Provide


suitable code if required.

Q18. How local static variable is different from global static variable?

Q19. Why we cannot create a private and protected class in Java?

Q20. What happens if we add final keyword with Class, Method and a
variable in java. Explain it through suitable code if required.

Common questions

Powered by AI

In Java, a ternary operator simplifies conditional operations in single lines by using syntax: condition ? expressionIfTrue : expressionIfFalse. For instance, if a>b, a ternary operator could call sum(a, b), otherwise sub(a, b). This is more concise compared to if-else, which would require separate lines: if (a > b) { sum(a, b); } else { sub(a, b); }. The ternary operator provides a streamlined conditional evaluation .

In Java's multi-level inheritance, constructors in the hierarchy are called in top-down order, starting with the base class. For ClassThree, which is a subclass of ClassTwo, and ClassOne being the base: ClassOne has a parameterized constructor, ClassTwo a default constructor, and ClassThree both types. When ClassThree objects are created, for example ClassThree obj = new ClassThree(1), it first calls ClassOne's constructor, then ClassTwo's, and finally ClassThree's constructor with parameters .

Nested inheritance in Java, as with ClassThree extending ClassTwo which extends ClassOne, dictates that top-level constructors are executed first. If a ClassThree object is instantiated, ClassOne, being the top ancestor with a parameterized constructor, initializes first, followed by ClassTwo's default constructor, then ClassThree's own constructors, maintaining a bottom-up instantiation hierarchy while constructors are triggered top-down .

Java disallows private and protected classes at the top level because classes are intended to be accessed at relevant scopes when declared. The intended alternative is using nested classes, which can be declared private or protected within another class, allowing encapsulation at the suitable scope while maintaining accessibility control .

Yes, a private constructor can be created in Java to restrict instantiation directly from other classes. It's often used in the singleton design pattern to ensure only one instance of the class is created, or in utility classes, where all methods are static and instantiation is unnecessary .

A 100% abstract class can include fields and has a constructor, providing common state and behavior for subclasses, but forces subclasses to implement abstract methods. An interface, especially a functional one, defines contract purely without state, suitable for functionality without predefined data. Use an abstract class for shared states and varying behavior; an interface when implementing interchangeable roles across hierarchies without state .

The final keyword in Java restricts the modification of the entity it is applied to. When applied to a class, it prevents subclassing, making the class non-extendable. Example: final class FinalClass {}. When applied to a method, it prevents overriding in subclasses, maintaining the method implementation in all subclasses. Example: final void methodName() {}. For a variable, it makes the variable constant, whose value cannot be changed once assigned. Example: final int CONSTANT = 100. The final keyword thus ensures consistency and immutability for these entities .

A functional interface in Java, like Runnable, contains a single abstract method which defines the expected behavior; useful for functional programming and lambda expressions. A marker interface, such as Serializable, contains no methods and signifies additional metadata needed for runtime. Different use cases make functional interfaces crucial for behavior specification while marker interfaces serve as annotations .

Local static variables are not supported in Java because static pertains to class level, and local variables are method-specific, existing while the method is on stack. Global static variables, by contrast, exist per class, shared by all instances. They retain values across object instances and are initialized when the class is loaded. This design prevents unintended changes and ensures memory efficiency for class-wide constants .

In Java, including a return type in a constructor signature misclassifies it as a method. Constructors inherently do not return objects; they initialize them. Adding a return type causes compilation errors since the constructor signature should match the class name without any return type for correct object instantiation .

You might also like