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

MCS-024 Solved Assignment 2023-2024

The document contains an assignment for an Object Oriented Technologies and Java Programming course. It includes 4 questions about abstraction, interfaces, threads, and the differences between AWT and Swing. Students are asked to write solutions for each question in 4-5 sentences explaining the concepts and providing examples where needed.

Uploaded by

dollarsage
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)
93 views

MCS-024 Solved Assignment 2023-2024

The document contains an assignment for an Object Oriented Technologies and Java Programming course. It includes 4 questions about abstraction, interfaces, threads, and the differences between AWT and Swing. Students are asked to write solutions for each question in 4-5 sentences explaining the concepts and providing examples where needed.

Uploaded by

dollarsage
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
You are on page 1/ 12

MCS-024 Solved Assignment 2023-2024 (July-January)

Course Code MCS-024

Course Title Object Oriented Technologies and Java Programming

Assignment Number BCA (IV)/-024/Assignment/2023-24

Maximum Marks 100

Weightage 30%

31st October, 2023 (For July Session)


Last Date of Submission
30 th April, 2024 (For January Session)

Note: There are Four questions in this assignment which carried 80 marks. Rest 20 marks are for viva-voce. Answer all the
questions. Give appropriate comments in programs to increase understandability. Wherever required, you may write java
program, run it on machine and take its output as part of solution. Please go through the guidelines regarding assignments
given in the Program Guide for the format of presentation.

Q1. What is Abstraction? How does Object Oriented Programming differ from Procedural Programming? Explain with examples. (20)

Q2. What is meant by interface in Java? Explain its use with an example. (20)

Q3. What is meant by thread in Java? Explain its use with an example. (20)

Q4. What is AWT? How does it differ from swing ? (20)

-: Solution :-
Q1. What is Abstraction? How does Object Oriented Programming differ from Procedural Programming? Explain with examples. (20)

Solution:
Imagine I'm a student who loves building stuff with LEGO bricks. Abstraction is like my superpower to create complex LEGO structures without
worrying about every little detail. I can build a spaceship using various LEGO pieces, and all I need to know is how to put them together – I don't
need to understand how each piece is made.

Abstraction in programming is quite similar. It's about focusing on what things do rather than how they do it. When I'm programming, I can
create "blueprints" for objects, like cars or animals, and I only need to know how to use them. I don't need to know the internal nitty-gritty details
of how they work.

Now, let's roll with some examples to explore how Object-Oriented Programming and Procedural Programming differ:

Object-Oriented Programming (OOP):

Imagine I'm coding a virtual zoo. In OOP, I'll treat everything like real-world objects. I'll have classes for animals, each with their unique
characteristics. For instance, I'll have a Lion class with methods like roar() and hunt(). I don't need to know how a lion roars internally – I just
know I can call the roar() method and get the expected result. Here's a snippet:

class Lion {

void roar() {

System.out.println("Roarrr!");

void hunt() {

System.out.println("Lion is hunting.");

public class VirtualZoo {

public static void main(String[] args) {

Lion lion = new Lion();


lion.roar(); // I call the roar method without knowing how it's done inside.

Procedural Programming:

Now, let's step into the procedural world. Here, I'll be more like a recipe follower. If I'm creating a "roaring lion" program, I need to explicitly tell
every step – open the mouth, create a sound, etc. It's like baking cookies following a detailed recipe. Here's a glimpse:

public class RoaringLion {

public static void main(String[] args) {

openMouth();

makeSound();

static void openMouth() {

System.out.println("Lion opens its mouth.");

static void makeSound() {

System.out.println("Roarrr!");

So, in OOP, I encapsulate behaviors within objects and focus on their usage. In procedural programming, I spell out each step like a detailed
instruction manual.
Q2. What is meant by interface in Java? Explain its use with an example. (20)

Solution:

Imagine I'm a student who loves music. I've decided to build a virtual music player program, and I want it to be super flexible. I want to be able to
add different types of music sources, like streaming services and local files, without making my program a tangled mess. That's where interfaces in
Java come in – they're like the ultimate playlist organizer!

An interface in Java is like a music genre that all my favorite songs follow. Each genre has its own vibe, just like each interface has its own set of
methods. So, if I want to create different music sources, I'll use interfaces to lay down the ground rules.

Let's groove with an example:

Imagine I'm coding my music player, and I want to handle both streaming services and local files. I'll create two interfaces, StreamingService and
LocalFile:

public interface StreamingService {

void playOnline(); // All streaming services can play online tracks

void skipNext(); // Skipping to the next track is common too

public interface LocalFile {

void playOffline(); // Local files can be played offline

void pause(); // Pausing the track is also a must

These interfaces are like playlists that my music sources need to follow. Every streaming service needs to play online and skip tracks, while local files
need to play offline and pause.

Now, let's add some music sources:

public class Spotify implements StreamingService {


@Override

public void playOnline() {

System.out.println("Spotify is playing online track.");

@Override

public void skipNext() {

System.out.println("Skipping to the next track on Spotify.");

public class LocalMP3 implements LocalFile {

@Override

public void playOffline() {

System.out.println("Playing local MP3 track offline.");

@Override

public void pause() {

System.out.println("Pausing the local MP3 track.");

}
In this example, both Spotify and LocalMP3 classes follow the rules of their respective interfaces. They implement the required methods. But here's
the magic: even though they're different types of music sources, they're all playing by the same interface rules. It's like having a mix of songs from
different artists, but they all follow the same musical structure.

Q3. What is meant by thread in Java? Explain its use with an example. (20)

Solution:

Imagine I'm a student with a bunch of assignments to tackle. Each assignment takes its own time, but waiting for one to finish before moving to the
next? Not my style. So, here's the genius move – I decide to work on multiple assignments simultaneously. That's the thread magic happening in
Java!

Threads in Java are like having a bunch of mini-me workers that help my program do things all at once. Instead of a single task taking over, I can
split my tasks into threads, and they work together like a team. It's like having a study group where everyone takes on different subjects, and we're
all making progress together.

Here's a simple example to make this even clearer, using a scenario we can all relate to – running a pizza joint:

Imagine I'm running this epic pizza place. Customers are lining up, and I've got orders to take, pizzas to toss, and deliveries to make. Instead of
handling one thing at a time, I can call in threads to the rescue! One thread is taking orders, another is making pizzas, and a third is handling
deliveries. This way, I'm not making folks wait, and we're serving up slices like champs.

Check out this snippet to see threads in action:

PizzaShop.java

public class PizzaShop {

public static void main(String[] args) {

Thread orderThread = new Thread(() -> {


System.out.println("Taking orders from hungry customers.");

});

Thread tossThread = new Thread(() -> {

System.out.println("Flipping and tossing pizza dough like a pro.");

});

Thread deliveryThread = new Thread(() -> {

System.out.println("Speeding off to deliver piping hot pizzas.");

});

// Time to start the threads!

orderThread.start();

tossThread.start();

deliveryThread.start();

Screenshot of Output:
In this code snippet, each thread represents a task in my pizza shop – taking orders, tossing dough, and delivering pizzas. When I fire up the
threads, they're like my trusty pizza-making crew, all doing their thing at once.

Q4. What is AWT? How does it differ from swing? (20)

Solution:

AWT (Abstract Window Toolkit) is a Java- grounded toolkit that provides a set of classes and styles for creating graphical user interfaces(
GUIs) in Java operations. It serves as the foundation for erecting windows, buttons, menus, and other visual factors in Java programs. AWT is part
of the core Java API and offers a way to produce platform-independent user interfaces.

AWT factors are erected on top of the native factors handed by the underpinning operating system. This means that AWT GUI factors have a look
and feel harmonious with the host operating system. AWT leverages the native GUI toolkit of the beginning platform, allowing Java operations to
mix in seamlessly with the native terrain.

On the other hand, Swing is a more advanced and flexible GUI toolkit also handed by Java. Swing is erected on top of AWT but offers a richer set of
factors and lesser customization options. Swing factors are entirely written in Java and aren't dependent on the native operating system factors.
This results in a harmonious appearance across different platforms, frequently appertained to as the"cross-platform" or" pluggable look and feel."

The crucial differences between AWT and Swing include

1. Look and Feel

 AWT factors use the native GUI toolkit of the beginning platform, furnishing a look and feel harmonious with the host operating system.
 Swing factors are designed to have a harmonious appearance across different platforms, offering a more invariant look and feel.

2. Customization
 AWT factors have limited customization options compared to Swing.
 Swing factors offer expansive customization, allowing inventors to produce unique and ultramodern user interfaces.

3. Performance

 AWT factors generally offer better performance due to their use of native factors.
 Swing factors might be slightly slower due to the fact that they're entirely enforced in Java and not grounded on native factors.

4. Vacuity

 • AWT factors are available in all Java executions since it's part of the core Java API.
 • Swing factors need to be included independently, but they're also extensively available and generally used.

5. Complexity

 AWT is simpler and offers smaller factors compared to Swing.


 Swing is more complex but provides a broader range of factors and features.

Let's first write the Java program that demonstrates the difference between AWT and Swing. Then, I'll provide explanation for each part.

AWTvsSwingDemo.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class AWTvsSwingDemo {


public static void main(String[] args) {

// AWT Example

Frame awtFrame = new Frame("AWT Example");

Button awtButton = new Button("AWT Button");

awtFrame.add(awtButton);

awtFrame.setSize(300, 200);

awtFrame.setVisible(true);

awtFrame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

});

// Swing Example

JFrame swingFrame = new JFrame("Swing Example");

JButton swingButton = new JButton("Swing Button");

swingFrame.add(swingButton);

swingFrame.setSize(300, 200);

swingFrame.setVisible(true);

swingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Screenshot of Output:

AWT Example:

So, let's say I want to create a window using AWT. It's like building a frame for a cool picture. First, I make an "AWT Example" window and then
add an "AWT Button" inside it. It's like adding a button to my frame. Next, I decide how big the window should be, so I make it 300 pixels wide
and 200 pixels tall. I can see my window by making it visible!

Swing Example:

Now, let's talk about the Swing window. It's like getting a brand-new canvas to create my window. Just like before, I'm adding a button – this time
it's a "Swing Button" – inside my Swing window. I'm also deciding how big my window should be, so I set it to 300 pixels wide and 200 pixels tall.
And the magic touch? I make the window visible, and I don't even need to worry about special code for closing. I tell my program, "Hey, if someone
closes this window, let's end the whole thing smoothly." It's like turning off the lights after an awesome party – no fuss, no mess!
For more solved assignments, WhatsApp me @ 7980608289

Learning Science
https://learningscience.co.in
Thank You

You might also like