MCS-024 Solved Assignment 2023-2024
MCS-024 Solved Assignment 2023-2024
Weightage 30%
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)
-: 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:
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.");
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:
openMouth();
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.
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:
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.
@Override
@Override
@Override
}
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.
PizzaShop.java
});
});
});
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.
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."
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
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.*;
// AWT Example
awtFrame.add(awtButton);
awtFrame.setSize(300, 200);
awtFrame.setVisible(true);
awtFrame.addWindowListener(new WindowAdapter() {
System.exit(0);
});
// Swing Example
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