Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
33 views2 pages

BIT 2203 Assignment1

Uploaded by

ianwanjohi20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

BIT 2203 Assignment1

Uploaded by

ianwanjohi20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

BIT 2303 Assignment 1

1) Classes and interfaces


a) Create a class hierarchy representing vehicles. Start with a base class Vehicle, which has
attributes like make, model, and year. Create two subclasses, Car and Motorcycle, that inherit
from Vehicle. Implement constructors(overloaded) for each class and include additional
attributes specific to each type of vehicle, such as numDoors for cars and isSportBike for
motorcycles. Include at least 4 attributes for each of the subclasses. Implement appropriate
accessor and mutator methods for the subclass. For constructors and mutator methods the
parameters should be validated accordingly. Override the toString() method in each subclass to
display detailed information about the vehicle, including the inherited attributes from the base
class. Write a driver program(with main method) to provide code examples that create instances
of both car and motorcycle objects, uses the java.util Scanner class to input object's attributes,
invokes the accessor and mutator methods and demonstrate the overridden toString() method
for the subclasses by displaying the vehicle details on the screen using an appropriate GUI
component. [8 marks]

b) Design a system for a library catalog. Create an interface Readable with a method getInfo() to get
information about a book. Then, create two classes, Book and EBook, both of which implement
the Readable interface. Implement the getInfo() method differently in each class to provide
information specific to physical books and e-books. Finally, create a library catalog that stores an
array of Readable objects and demonstrate polymorphism by calling the getInfo() method on
each item in the catalog. [6 marks]

2) Strings
i) Given the following string:

String str= “Baseball park”;


a) The first occurrence of ‘l’ in str
b) Find and replace ‘base ’ with ‘foot’
c) Extract and display ‘park ’ from str
d) Replace park with ‘Stadium’ [4 marks]
ii) Write a program that inputs from the user a file pathname and extracts: path, filename and
extension.
Example: pathname: \programs\java\testProgram.java
Path: \programs\java
Filename: testProgram
Extension: java [4 marks]

iii) Write and test a program having a method newString () that takes two string objects as
arguments. The string compares (lexicographically) the two string arguments and returns
the concatenation of the arguments with the lesser string coming first. [3 marks]
iv) Write a program with a method strInsert() that takes str1 and str2 and an integer pos, as
arguments. The method inserts str2 in str1 beginning at position pos provided that pos <
str1.length().Otherwise str2 is inserted at the end of str1.Implement the methods using
substring and ‘+’ methods. [3 marks]
v) A method strReplace takes strings str1, str2 and str3 as arguments. In str1, the method
replaces all occurrences of str2 with str3. Implement using appropriate String class methods.
In the main method as ask for input from the keyboard using an input dialog and display the
resulting string using a message dialog. [4 marks]

3) File Handling and Exception

The file applications below should appropriately include exception handling

a) Using a file named “mydata.txt” demonstrate read/write operations using each of the following
java.io classes listed below. Write a separate application for each section.

i) FileInputStream and FileOutputStream

ii) DataInputStream and DataOutputStream

iii) BufferedReader

iv) PrintWriter

[12 marks]

b) Using ObjectInputStream and ObjectOutputStream write a program to write vehicle objects


described in 1a) above. [6 marks]

You might also like