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

Assignment 8.1 (Exceptions) : Myintegers - Java

The document outlines a series of programming assignments focused on exception handling in Java. It includes tasks such as creating classes to handle arrays of integers and strings, reading command line inputs, and implementing file read/write operations with exception management. Additionally, it suggests enhancing an existing program to incorporate these functionalities, ensuring data persistence through file operations.

Uploaded by

semacigerimsin
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 views2 pages

Assignment 8.1 (Exceptions) : Myintegers - Java

The document outlines a series of programming assignments focused on exception handling in Java. It includes tasks such as creating classes to handle arrays of integers and strings, reading command line inputs, and implementing file read/write operations with exception management. Additionally, it suggests enhancing an existing program to incorporate these functionalities, ensuring data persistence through file operations.

Uploaded by

semacigerimsin
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/ 2

University of Applied Sciences Augsburg Prof. Dr.

Jens Lauterbach

Assignment 8.1 (Exceptions)


Write a class MyIntegers.java that creates an array of integers with length 20. Assign a value to that
array at index 20 (e.g., myIntegers[20] = 199). Change your program to catch the exception created.

Assignment 8.2 (Exceptions)


Write a class MyStrings.java that creates an array of Strings with length 20. Make sure your array is
empty / optionally initialize the array with null values e. g., myStrings[n] = null. Print out the length of
the value of the array at position 0. Change your program to catch the exception created.

Assignment 8.3 (Exceptions)


Create a program ReadCommand.java that reads Integers from the command line (parameters of
main method!) and prints them out on the console. After that, try to run it without entering any
numbers in the command line or using an invalid value such as "Hello" instead of a whole number.
Change your program to catch the exception created.

Assignment 8.4 (Read/Write to File with Exceptions) - OPTIONAL


Your BestBike program from the previous assignments is working. However, it has a big weakness,
the component information the user enters, and processes gets lost, when the user leaves the
program. With the concept of exceptions at hand, you can now solve this problem.

Write a class ReadWriteComponents that has three class methods, componentReader(),


componentDataToString() and writeComponents() that will allow you read and write your
components in a file on your file system.

The method componentReader() reads component data from a file and has the following
functionality:

• It has no return value and receives a file name as String and an array of component objects
as parameters
• Use the following variables for your component objects (Use only String variables for this
assignment)
String component_ID = "0";
String name = "";
String delivery_date = "19000101";
String stock_amount = "0";
String vendor = "";
String comment = "";
String componentMaterial;

• Use a file components.csv (the file needs to be stored in the folder of your Java project e. g.,
C:\...\eclipse-workspace\<YourProjectFolder>) with comma separated values and
the following data.
;1;wheel;20211201;11;GreatWheels;good deal ;3;eor

;2;brake;20211202;12;GreatBrakes;better deal ;2;eor

;3;Handlebar;20211203;13;GreatHandlebars;best deal;1

1
Programming 1 – JAVA
University of Applied Sciences Augsburg Prof. Dr. Jens Lauterbach

• The data of the file corresponds with the component variables above. Values are separated
by “;”. The end of a row that corresponds with the data of one component object is denoted
by “eor”
• Use the class File (package java.io.) to create a file with the file name as constructor
parameter
• Use the class Scanner in the java.util package and its methods to read the data from the file and
store the data values in your component object attributes (you need three component objects in
your array to work with the file)
• Write a test class (with main method) to create an array of components and test the
componentReader() method
• The method throws a FileNotFoundException, handle it in your test class

The method componentDataToString() takes the data stored in your component objects and
transforms it into a comma separated String with the following functionality:

• It has a StringBuffer object (see class StringBuffer) as return value and receives an array of
component objects as parameter
• Use a method of StringBuffer to read the data from your component objects and concatenate it
to a String
• The String should have the same format/data structure as the components.csv file used in the
componentReader() method
• Test the method in your test class and print out the result on the screen

The method writeComponents() writes the data that the method componentDataToString() returns
into a new file componentsOutput.csv

• The method has no return value and receives a StringBuffer object and a Writer object (see class
Writer in the package java.io) as parameters
• Use a method of the StringBuffer class to write the StringBuffer object into a String variable
• Write the content of the String variable into the componentsOutput.csv
• Test your method in the test class. In the test class you need to create a new file object with
componentsOutput.csv as parameter. Pass this file to a new object of the Filewriter class.
• Pass your method writeComponents() the StringBuffer object and the Filewriter object as
parameters
• Check if the file componentsOutput.csv was created and contains the correct data
• The method throws an IOException, handle it in your test class

Assignment 8.5 (Read/Write to File with Exceptions) - OPTIONAL


Adjust your BestBike program (the version from CC6) so that it can now work with the
ReadWriteComponents class and its methods.

When starting the program, the data will be read from a file components.csv.

When ending the program, the user will be asked if the changes made to component objects should
be saved or discarded. When the user chooses yes, the data will be saved to components.csv.

2
Programming 1 – JAVA

You might also like