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

BIT 2nd Sem - JAVA Programming, Assignment 2_1724306807565

The document outlines an assignment for Java programming that includes both theoretical and practical portions. It covers topics such as the differences between String and StringBuffer, exception handling, input/output streams, Java collections, and design patterns. Students are required to write programs and provide explanations for various concepts and scenarios in Java.

Uploaded by

bstahome11
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)
8 views

BIT 2nd Sem - JAVA Programming, Assignment 2_1724306807565

The document outlines an assignment for Java programming that includes both theoretical and practical portions. It covers topics such as the differences between String and StringBuffer, exception handling, input/output streams, Java collections, and design patterns. Students are required to write programs and provide explanations for various concepts and scenarios in Java.

Uploaded by

bstahome11
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/ 7

Texas College of Management and IT

Java Programming
Explore, Learn and Excel

Assignment 2
Theory portion

● Explain the differences between String and StringBuffer classes in Java, and in what
scenarios you should use each one?
● Explain how to convert an object to a string in Java. Explain different access modifiers
with their scope and example.
● Write an immutability concept of String with the help of an example, program, and
diagram.
Note: (Heap Memory and SCP)

Practical Portion

● Write a program to create String Object


 by string literal
 new keyword
● Write a simple code

 Employee ‘e’ object

 Convert ‘e’ object to String


Note (Example of convert String to Object in java.)
● Write a simple code which contains
 String one

 String two

 Another String three which concat one and two


Note: (Example of String concatination)
● Write a simple code which contains

 String name

 Calculate the length of string

● Write a simple code which contains

 String one = "abc"; String two = "def";

String three = "abc";

String four = "ABC";


 Compare String one with other string and print out its result.
Exception Handling

Theory portion

● Explain the concept of Exception Handling in Java and discuss some best
practices for using Exceptions in your code with checked and unchecked
exception.
● Explain try, catch and finally block with example.
● How can you create your own exception? Explain with example.
● Give clear explanation of “throw” and “throws” keyword which are used in
exception handling with example.
Practical Portion

● Write a program to handle Arithmetic Exception, I/O Exception, Null Pointer


Exception.

● Write an example of ArrayIndexOutOfBoundException.

● The following program will generate an error, because myNumbers[5] does not exist.

public class Main {


public static void
main(String[ ] args) {
int[] myNumbers = {1, 2,
3};

System.out.println(myNumbers[5]); // error!

}
}

 Use try...catch to catch the error and execute some code to handle it:

● Write a program to throw an exception if:

 age is below 18 (print "Access denied").

 if age is 18 or older, print "Access granted":


Note: Use throw Keyword

 Create a Java program that demonstrates the use of a custom exception.


Design a custom exception class named InvalidInputException and use
it to handle a scenario where the user enters an invalid input (e.g., a
negative number) for a simple calculation.
Input and Output Stream

Theory Portion

● What is common and how do the following streams differ: InputStream,


OutputStream, Reader, Writer?

● How do you serialize objects in Java? Explain with examples.

● Illustration of understanding of byte Stream and Character stream in Java with


examples.

Practical Portion

● Write a Java program that reads a text file and counts the number of
words in it. The program should then write the word count to a new file.

● Create a class called Student to represent a student. A Student should


include four pieces of information as instance variables: a student name,
a roll number, a semester and an address. Your class should have a
constructor that initialises the four instance variables. Provide a mutator
method and accessor method (query method - getter and setter) for each
instance variable. In Addition, provide a method named getStudnetInfo
that returns the description of the student as a String (the description
should include all the information about the book). Write Program to get
input of student from user and store the information in file called
“student.txt”.

● Create a class called Book to represent a book. A Book should include


four pieces of information as instance variables: a book name, an ISBN
number, an author name and a publisher. Your class should have a
constructor that initialises the four instance variables. Provide a mutator
method and accessor method (query method - getter and setter) for each
instance variable. In Addition, provide a method named getBookInfo
that returns the description of the book as a String (the description should
include all the information about the book). Write Program to get input
of book from user and store this information in file called “book.txt”.
Java Collection

Theory Portion

● What is the Java Collection Framework and how do you choose different collections?
Explain Generic in Java.

● Explain scenarios to use Set, List and Map in Java with proper reasons and examples.

● What do you mean by thread-safe collection? Give an example of 2 thread-safe Collection in


Java?

● Explain Linked List and Array List with examples.

Practical Portion

● Write a Java program to create a new array list, add some colors (string) and print
out the collection.

● Write a Java program to iterate through all elements in a linked list.

● Write a Java program to create a new priority queue, add some colors (string) and
print out the elements of the priority queue.
Design Pattern

Theory portion

● Explain your understanding in Design Patterns with real life examples and types.
● Give an example of a singleton in java with a proper example.
● Design patterns ease the analysis and requirement phase of SDLC by providing
informationbased on prior hands-on experiences. Explain

Practical portion

Suppose you want to create a class for which only a single instance (or object) should be
createdand that single object can be used by all other classes. Propose a solution for this
problem.

You might also like