0% found this document useful (0 votes)
14 views19 pages

Java Day 1: Theory & Program Solutions

Uploaded by

Sofia lourdu
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)
14 views19 pages

Java Day 1: Theory & Program Solutions

Uploaded by

Sofia lourdu
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

Java Task Solutions – Day 1 (Theory +

Programs)
This document covers Day 1 of the uploaded "Java [Link]": Java Introduction,
Class/Method/Object, and Same/Different package. It provides concise theory answers and
complete Java programs with different approaches and expected outputs.

Day 1 – Theory Answers


Item Answer / Notes

1) What is Java? Java is an object-oriented, class-based,


strongly typed programming language and
platform. It compiles to bytecode that runs
on the Java Virtual Machine (JVM).

2) Why do we go for Java? Portability (write once, run anywhere),


strong standard library, OOP features,
automatic memory management (GC), rich
tooling & ecosystem, and strong
community.

3) Features of Java Object-oriented, Platform-independent (via


JVM), Robust (exception handling, GC),
Secure (sandboxing, bytecode verifier),
Multithreaded, Distributed, High
performance (JIT).

4) Why is Java platform independent? The Java compiler produces bytecode


(.class) that runs on any JVM
implementation for any OS/CPU, so the
same program works across platforms.

5) Explain JDK, JVM, JRE JDK: developer kit (compiler, tools, JRE).
JRE: runtime (JVM + core libraries). JVM:
engine that loads, verifies, and executes
bytecode.

6) Explain Standard Notation Follow Java naming & package conventions:


packages lowercase ([Link]), classes
PascalCase (StudentInfo), methods
camelCase (stuName), constants
UPPER_SNAKE_CASE.

7) Explain OOPs Four pillars: Encapsulation (data hiding via


classes), Inheritance (code reuse),
Polymorphism (same interface, many
forms), Abstraction (essential features,
hide details).

8) Explain class, methods, object Class: blueprint; Method:


behavior/function; Object: runtime
instance with state + behavior created from
a class.

9) Syntax of object creation ClassName ref = new ClassName(); // calls


zero-arg constructor

10) Syntax of import import [Link]; or


import packageName.*;

Day 1 – Programs (Multiple Methods + Expected Outputs)


All programs follow coding standards: proper packages, PascalCase classes, camelCase
methods, and meaningful outputs.

Q1) Project: StudentDetails ([Link])


➤ Method A: Simple printer methods (void) called on an object

Method A – Code

package [Link];

public class StudentInfo {

public void stuId() { [Link]("ID : 1024"); }

public void stuName() { [Link]("Name : Anitha"); }

public void stuDob() { [Link]("DOB : 2001-05-21"); }

public void stuPhoneNumber() { [Link]("Phone:


9876543210"); }
public void stuEmail() { [Link]("Email:
anitha@[Link]"); }

public void stuAddress() { [Link]("Addr : 12, Gandhi St,


Chennai"); }

public static void main(String[] args) {

StudentInfo s = new StudentInfo();

[Link]();

[Link]();

[Link]();

[Link]();

[Link]();

[Link]();

Expected Output (sample):

ID : 1024

Name : Anitha

DOB : 2001-05-21

Phone: 9876543210

Email: anitha@[Link]

Addr : 12, Gandhi St, Chennai

➤ Method B: Return values + formatted print in main

Method B – Code

package [Link];

public class StudentInfo {

public int stuId() { return 1024; }

public String stuName() { return "Anitha"; }


public String stuDob() { return "2001-05-21"; }

public long stuPhoneNumber() { return 9876543210L; }

public String stuEmail() { return "anitha@[Link]"; }

public String stuAddress() { return "12, Gandhi St, Chennai"; }

public static void main(String[] args) {

StudentInfo s = new StudentInfo();

[Link]("ID : %d%n", [Link]());

[Link]("Name : %s%n", [Link]());

[Link]("DOB : %s%n", [Link]());

[Link]("Phone: %d%n", [Link]());

[Link]("Email: %s%n", [Link]());

[Link]("Addr : %s%n", [Link]());

Expected Output (sample): same as Method A

➤ Method C: Use constructor + fields + getters (encapsulation)

Method C – Code

package [Link];

public class StudentInfo {

private int id;

private String name, dob, email, address;

private long phone;

public StudentInfo(int id, String name, String dob, long phone,


String email, String address) {

[Link] = id; [Link] = name; [Link] = dob;


[Link] = phone; [Link] = email; [Link] = address;

public int getId() { return id; }

public String getName() { return name; }

public String getDob() { return dob; }

public long getPhone() { return phone; }

public String getEmail() { return email; }

public String getAddress() { return address; }

public static void main(String[] args) {

StudentInfo s = new StudentInfo(1024, "Anitha", "2001-05-21",

9876543210L, "anitha@[Link]", "12, Gandhi St,


Chennai");

[Link]("ID : " + [Link]());

[Link]("Name : " + [Link]());

[Link]("DOB : " + [Link]());

[Link]("Phone: " + [Link]());

[Link]("Email: " + [Link]());

[Link]("Addr : " + [Link]());

Expected Output (sample): same as above.

Q2) Project: BankDetails ([Link])


➤ Method A: Simple printer methods

Method A – Code

package [Link];
public class BankInfo {

public void fullName() { [Link]("Full Name : Priya


K"); }

public void sortCode() { [Link]("Sort Code :


HDFC0001234"); }

public void accountNumber() { [Link]("Account No :


123456789012"); }

public void bankAddress() { [Link]("Branch Address: 1/2


Gandhi Rd, Chennai"); }

public static void main(String[] args) {

BankInfo b = new BankInfo();

[Link]();

[Link]();

[Link]();

[Link]();

Expected Output (sample):

Full Name : Priya K

Sort Code : HDFC0001234

Account No : 123456789012

Branch Address: 1/2 Gandhi Rd, Chennai

➤ Method B: Return values + printf

Method B – Code

package [Link];

public class BankInfo {

public String fullName() { return "Priya K"; }

public String sortCode() { return "HDFC0001234"; }


public String accountNumber() { return "123456789012"; }

public String bankAddress() { return "1/2 Gandhi Rd, Chennai"; }

public static void main(String[] args) {

BankInfo b = new BankInfo();

[Link]("Full Name : %s%n", [Link]());

[Link]("Sort Code : %s%n", [Link]());

[Link]("Account No : %s%n", [Link]());

[Link]("Branch Address: %s%n", [Link]());

Expected Output: same as Method A.

Q3) Project: MyPhone ([Link])


➤ Method A: Simple printer methods

Method A – Code

package [Link];

public class PhoneInfo {

public void phoneName() { [Link]("Phone : Pixel 8"); }

public void phoneMieiNum() { [Link]("IMEI :


356789123456789"); }

public void Camera() { [Link]("Camera: 50MP + 12MP"); }

public void storage() { [Link]("Storage: 256GB"); }

public void osName() { [Link]("OS : Android 14"); }

public static void main(String[] args) {

PhoneInfo p = new PhoneInfo();

[Link]();
[Link]();

[Link]();

[Link]();

[Link]();

Expected Output (sample):

Phone : Pixel 8

IMEI : 356789123456789

Camera: 50MP + 12MP

Storage: 256GB

OS : Android 14

Q4) Project: CompanyDetails ([Link])


Method – Code

package [Link];

public class Company {

public void companyName() { [Link]("Company : ABC Tech


Pvt Ltd"); }

public void companyId() { [Link]("CompanyId: 98765"); }

public void companyAddress() { [Link]("Address : 22,


OMR, Chennai"); }

public static void main(String[] args) {

Company c = new Company();

[Link]();

[Link]();

[Link]();
}

Expected Output (sample):

Company : ABC Tech Pvt Ltd

CompanyId: 98765

Address : 22, OMR, Chennai

Q5) Project: LanguageDetails ([Link]) – Composition inside


LanguageInfo
Code

package [Link];

class StateDetails {

public void southIndia() { [Link]("South: Tamil, Telugu,


Kannada, Malayalam"); }

public void northIndia() { [Link]("North: Hindi,


Punjabi, Marathi, Gujarati"); }

public class LanguageInfo {

private StateDetails state = new StateDetails();

public void tamilLanguage() { [Link]("Tamil


Language"); }

public void englishLanguage() { [Link]("English


Language"); }

public void hindiLanguage() { [Link]("Hindi


Language"); }

public static void main(String[] args) {

LanguageInfo li = new LanguageInfo();


[Link]();

[Link]();

[Link]();

[Link]();

[Link]();

Expected Output (sample):

Tamil Language

English Language

Hindi Language

South: Tamil, Telugu, Kannada, Malayalam

North: Hindi, Punjabi, Marathi, Gujarati

Q6) Project: PhoneDetails ([Link]) – Composition inside


InternalStorage
Code

package [Link];

class ExternalStorage {

public void size() { [Link]("External Storage: 512GB


microSD"); }

public class InternalStorage {

private ExternalStorage ext = new ExternalStorage();


public void processorName() { [Link]("Processor:
Snapdragon 8 Gen 3"); }

public void ramSize() { [Link]("RAM : 12GB"); }

public static void main(String[] args) {

InternalStorage is = new InternalStorage();

[Link]();

[Link]();

[Link]();

Expected Output (sample):

Processor: Snapdragon 8 Gen 3

RAM : 12GB

External Storage: 512GB microSD

Q7) Project: CollegeInformation ([Link]) – Create objects inside


College
Code

package [Link];

class College {

public void collegeName() { [Link]("College : GCT


Coimbatore"); }

public void collegeCode() { [Link]("Code : 1101"); }

public void collegeRank() { [Link]("Rank : 5"); }

class Student {

public void studentName() { [Link]("Student : Karthik");


}
public void studentDept() { [Link]("Dept : CSE"); }

public void studentId() { [Link]("ID : 220045"); }

class Hostel {

public void hostelName() { [Link]("Hostel :


Cauvery"); }

class Dept {

public void deptName() { [Link]("Department: Computer


Science"); }

public class Runner {

public static void main(String[] args) {

College c = new College();

Student s = new Student();

Hostel h = new Hostel();

Dept d = new Dept();

[Link](); [Link](); [Link]();

[Link](); [Link](); [Link]();

[Link]();

[Link]();

Expected Output (sample):

College : GCT Coimbatore

Code : 1101

Rank : 5
Student : Karthik

Dept : CSE

ID : 220045

Hostel : Cauvery

Department: Computer Science

Q8) Project: VehicleInformation – Create objects inside Vehicle


Code (Note: separate files/packages in real project)

package [Link];

class Vehicle {

public void vehicleNecessary() { [Link]("Vehicles are


essential for transport"); }

package [Link];

public class TwoWheeler {

public void bike() { [Link]("Bike"); }

public void cycle() { [Link]("Cycle"); }

package [Link];

public class ThreeWheeler {

public void auto() { [Link]("Auto"); }

package [Link];

public class FourWheeler {

public void car() { [Link]("Car"); }


public void bus() { [Link]("Bus"); }

// NOTE: In actual project, each package/class must be in its own file.

// Runner demo:

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class Runner {

public static void main(String[] args) {

Vehicle v = new Vehicle();

TwoWheeler t2 = new TwoWheeler();

ThreeWheeler t3 = new ThreeWheeler();

FourWheeler t4 = new FourWheeler();

[Link]();

[Link](); [Link]();

[Link]();

[Link](); [Link]();

Expected Output (sample):

Vehicles are essential for transport

Bike

Cycle
Auto

Car

Bus

Q9) Project: TransportInformation – Create objects inside Transport


Code (Note: separate files/packages in real project)

// Packages abbreviated into one listing for illustration

package [Link];

public class Transport { public void transportForm()


{ [Link]("Transport Form Submitted"); } }

package [Link];

public class Road {

public void bike(){ [Link]("Road Bike"); }

public void cycle(){ [Link]("Road Cycle"); }

public void bus(){ [Link]("Road Bus"); }

public void car(){ [Link]("Road Car"); }

package [Link];

public class Air {

public void aeroPlane(){ [Link]("Aeroplane"); }

public void heliCopter(){ [Link]("Helicopter"); }

package [Link];

public class Water {

public void boat(){ [Link]("Boat"); }

public void ship(){ [Link]("Ship"); }

}
// Runner

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class Runner {

public static void main(String[] args) {

Transport t = new Transport();

Road r = new Road();

Air a = new Air();

Water w = new Water();

[Link]();

[Link](); [Link](); [Link](); [Link]();

[Link](); [Link]();

[Link](); [Link]();

Expected Output (sample):

Transport Form Submitted

Road Bike

Road Cycle

Road Bus

Road Car

Aeroplane

Helicopter
Boat

Ship

Q10) Project: NetworkInformation – Create objects inside Wifi


Code

package [Link];

class Wifi { public void wifiName(){ [Link]("WiFi :


Home_5G"); } }

class MobileData { public void dataName(){ [Link]("Data :


4G-LTE"); } }

class Lan { public void lanName(){ [Link]("LAN :


OfficeLAN"); } }

class Wireless { public void modamName(){ [Link]("Modem:


NETGEAR XR"); } }

public class Runner {

public static void main(String[] args) {

Wifi w = new Wifi();

MobileData m = new MobileData();

Lan l = new Lan();

Wireless wl = new Wireless();

[Link]();

[Link]();

[Link]();

[Link]();

Expected Output (sample):


WiFi : Home_5G

Data : 4G-LTE

LAN : OfficeLAN

Modem: NETGEAR XR

Q11) Project: EmployeeInformation – Create objects inside Employee


Code (Note: separate files/packages in real project)

package [Link];

public class Employee { public void empName()


{ [Link]("Employee: Manoj"); } }

package [Link];

public class Company { public void companyName()


{ [Link]("Company : ABCTech"); } }

package [Link];

public class Client { public void clientName()


{ [Link]("Client : Initech"); } }

package [Link];

public class Project { public void projectName()


{ [Link]("Project : Apollo"); } }

// Runner

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];
public class Runner {

public static void main(String[] args) {

Employee e = new Employee();

Company c = new Company();

Client cl = new Client();

Project p = new Project();

[Link]();

[Link]();

[Link]();

[Link]();

Expected Output (sample):

Employee: Manoj

Company : ABCTech

Client : Initech

Project : Apollo

Reference: Questions sourced from the uploaded Java [Link] (Day 1).

Common questions

Powered by AI

Inheritance in Java allows a new class to inherit properties and behaviors (methods) from another class, promoting code reuse by extending existing class functionalities without rewriting code. For example, various projects such as CollegeInformation demonstrate object collaboration, where classes like College, Student, and Dept could potentially share common attributes or methods through inheritance, thus reducing redundancy and enhancing maintainability .

Constructors in Java are special methods used to initialize objects. They allow setting initial states for objects when they are being created. This is crucial for encapsulation, a key principle of OOP, as constructors can restrict access to the fields they initialize by using access modifiers, safeguarding object data integrity. For instance, in the "StudentInfo" class, fields such as 'name,' 'id,' and others are initialized in a constructor, then accessed through public getter methods for controlled access .

Java's naming conventions, such as lowercase packages (e.g., org.example), PascalCase for classes (e.g., StudentInfo), camelCase for methods (e.g., stuName), and ALL_CAPS for constants, are designed to enhance code readability and maintainability. They provide a consistent structure that makes it easier for programmers to follow and understand code, even if new to the codebase. This standardization reduces errors and eases collaboration across teams, thus maintaining the quality and reliability of Java projects .

Java's platform independence, enabled by the JVM and bytecode, significantly impacts global, multi-platform software development by reducing platform-specific development and testing costs. This 'write once, run anywhere' capability simplifies deployment across diverse operating systems, increasing market reach and application versatility. However, it can introduce performance variability and dependency on consistent JVM quality across platforms, necessitating additional considerations in the development process to ensure uniform behavior. Overall, platform independence is a strategic advantage but requires careful planning to fully leverage its benefits .

Java handles memory management through an automated process known as garbage collection, where the Java Virtual Machine automatically recycles memory once objects are no longer in use. This contrasts with languages like C/C++ that require manual memory management using functions like malloc() and free(). The automated approach in Java reduces the risk of memory leaks and pointer-related errors, thereby simplifying development and improving application stability and reliability .

Java's multithreading capabilities allow simultaneous execution of two or more threads, improving application performance and responsiveness, which is crucial in modern, resource-intensive applications. In distributed systems, multithreading facilitates concurrent processing, efficient resource utilization, and improved throughput, essential for handling complex computations and real-time operations. However, it also requires careful management of shared resources and thread synchronization to avoid issues like race conditions and deadlocks, necessitating robust design to utilize Java's multithreading effectively .

Java's strong standard library significantly boosts developer productivity and application robustness by providing pre-built, tested components for a wide range of programming tasks. This includes collections, concurrency, I/O, networking, and more. These libraries reduce the need for developers to reinvent the wheel, thus saving time and effort. They also contribute to robust applications as they are optimized and widely tested by the Java community. As a result, Java applications built with these libraries tend to be more reliable and maintainable .

Java includes encapsulation, inheritance, polymorphism, and abstraction as its key object-oriented features. Encapsulation involves hiding data through classes, inheritance allows code reuse by enabling a class to inherit fields and methods from another, polymorphism enables objects to be treated as instances of their parent class at runtime, and abstraction allows ignoring complex background details to focus on essential aspects. In comparison to procedural programming paradigms which focus on procedures and functions, Java's OOP paradigm organizes code around objects and data. This approach enhances modularity and reusability significantly over procedural paradigms .

Encapsulation in Java is the practice of bundling the data and methods that operate on the data within a single unit or class, typically achieved with access modifiers to restrict access to certain components. It's about data hiding to protect object integrity. In contrast, polymorphism allows objects to be treated as instances of their parent class, facilitating method overriding and the ability to process objects differently based on their actual form at runtime. While encapsulation focuses on protecting data, polymorphism is about flexibility in object handling .

The JVM is critical to Java's platform independence because it allows Java bytecode, which is the output of the Java compiler, to run on any operating system that has a JVM implementation. Bytecode is an intermediate representation of code that is not tied to any specific machine architecture, making it portable across platforms. The Java Runtime Environment (JRE) includes the JVM and core libraries, providing the necessary environment for executing Java applications. Together, they allow a Java application to 'write once, run anywhere' .

You might also like