Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

PRACTICE REPORT

JAVA PROGRAMMING

By :
Name : Nurul Aini
NIM : C2C022086

FACULTY OF ENGINEERING
INFORMATICS AND COMPUTER SCIENCE STUDY PROGRAM
SEMARANG MUHAMMADIYAH UNIVERSITY

i
PREFACE

Thank God for the presence of Allah SWT with all his power to make us the most
perfect creatures compared to His other creatures. Sholawat and greetings are always poured
out to our king Muhammad SAW, to his family, friends and all of his people, of whom, God
willing, we will be one of them. So that I can complete the assignment entitled "Data
Structure Practice" on time.
The purpose of writing this report is to fulfill an assignment in the Database
Foundations Practice course. In addition, this report also aims to add insight about databases
for readers and also for writers.
First of all, I would like to thank Basirudin Muhammad ZainudinAl Amin,
S.Kom.M.Kom as the Data Structure Practice Lecturer who has given this assignment so that
I can add knowledge and insight according to the field of study that I am pursuing. I also
thank all those who I cannot mention all of them, thank you for your help so that I can
complete this task.
I realize that in the preparation of this report there are still many shortcomings, so I
really hope for corrections and guidance from interested parties for the perfection of this
report.

Semarang, 13 October 2023

Nurul Aini

ii
TABLE OF CONTENS

PREFACE..................................................................................................................................ii
TABLE OF CONTENS............................................................................................................iii
BAB III......................................................................................................................................1
SECTION 3-1.............................................................................................................................1
1.1 Practice Section Objectives 3-1..............................................................................................1
1.2 Basic Theory Practice Section 3-1..........................................................................................1
1.3 Discussion Section 3-1...........................................................................................................3

iii
BAB III
SECTION 3-1

1.1Practice Section Objectives 3-1


The purpose of making a practicum report on the introduction of Generics that
after studying this chapter students are expected t able to :
 Can use enumerated types
 Can Create a custom generic class
 Can use the type interface diamond to create an object
 Can use generic methods
 Can use wildcards

1.2Basic Theory Practice Section 3-1


A. Enumerations
Enumerations (or enums) are a specification for a class where all
instances of the class are created within the class. Enums are a datatype that
contains a fixed set of constants. Enums are good to use when you already
know all possibilities of the values or instances of the class. If you use enums
instead of strings or integers you increase the checks at compile time. Can be
declared on their own or within another class.
Say we wish to store the type of bank account within our Account
Class. We could have Savings and Credit as possible options. As long as we
specify that the class is of type enum, we can create these account types inside
the class itself as if each was created outside of the class. Enums can make
your code a lot more readable. Example, rather than access a seemingly
random number in your code you could access a name that would add
meaning to what you are trying to achieve.
Enums are like any other class and can have fields, constructors and methods.
By default, enums don’t require a constructor definition, their default values
are always the string used in the declaration. You can add additional attributes
to the enum fields. You can define your own constructors to initialize the state
of enum types. Objects can be instantiated from an enum class by going
through its constructor.
B. Generic Classes
A generic class is a special type of class that associates one or more
non-specified Java types upon instantiation. This removes the risk of the
runtime exception “ClassCastException” when casting between different
types. Generic types are declared by using angled brackets - <> around a
holder return type. E.g. <E>. <E> is simply an example. We could have
written <T> , <Type1> or <Element1>. The most commonly used type
parameter names are:
− E - Element (used extensively by the Java Collections Framework)
− K - Key
− N - Number

1
− T - Type
− V - Value
− S,U,V etc. - 2nd, 3rd, 4th types
When working with generic types, remember the following the types
must be identified at the instantiation of the class. Your class should contain
methods that set the types inside the class to the types passed into the class
upon creating an object of the class. One way to look at generic classes is by
understanding what is happening behind the code. This code can be interpreted
as a class that creates two objects, Type1 and Type2. Type1 and Type2 are not
the type of objects required to be passed in upon initializing an object. They
are simply placeholders, or variable names, for the actual type that is to be
passed in :

These placeholders allow for the class to include any Java type; they
become whatever type is initially used at the object creation. Inside of the
generic class, when you create an object of Type1 or Type2, you are actually
creating objects of the types initialized when an Example object is created.

So far we have created Generic classes, but we can also create generic
methods outside of a generic class. Just like type declarations, method
declarations can be generic—that is, parameterized by one or more type
parameters. A type interface diamond is used to create a generic method.
C. Type Interface Diamond
A type interface diamond enables you to create a generic method as
you would an ordinary method, without specifying a type between angle
brackets. Why a diamond?. The angle brackets are often referred to as the
diamond <>. Typically if there is only one type inside the diamond, we use
<T> where T stands for Type. For two types we would have <K,T>.
You can use any non reserved word as the type holder instead of using
<T>. We could have used <T1>. By convention, type parameter names are
single, uppercase letters. This stands in sharp contrast to the variable naming
conventions that you already know about, and with good reason: Without this
convention, it would be difficult to tell the difference between a type variable
and an ordinary class or interface name.
D. Generic Wildcards
Wildcards with generics allows us greater control of the types we use.
They fall into two categories:
− Unbounded

2
- <?> denotes an unbounded wildcard, It can be used to represent any
type. Example, arrayList represents an arrayList of unknown type.
Public void printList(List list). This declares a list of unknown type as
the parameter. You could have written a generic method to achieve the
same result using the Type assigned in the generic class . Public void
printList(List list). In our simple example the wildcard is slightly better
as it identifies that the type is not used elsewhere. It is simply a method
to iterate through a list of unknown type.
− Bounded
- <? Extends type> denotes an Upper Bounded Wildcard. Sometimes we
want to relax restrictions on a variable. Lets say we wished to create a
method that works only on ArrayLists of numbers
−ArrayList<Integer>
−ArrayList<Double>
−ArrayList<Float>
We could use an upper bounded wildcard
- <? Super type> denotes a Lower Bounded Wildcard. A lower bounded
wildcard restricts the unknown type to be a specific type or a super
type of that type. Say a method is required that puts Integer objects into
an ArrayList. To maximize flexibility, you would like the method to
work on ArrayList, ArrayList, and ArrayList.
In the previous example where we only ever wanted to iterate through
an array we would only have wanted to limit this to the array type.

1.3Discussion Section 3-1


Tasks
Identify the vocabulary word for each definition below.
Generic Class This is a special type of class that associates one or
more non-specified Java types.
<> A type interface diamond is what 2 characters
Enumerations A datatype that contains a fixed set of constants

Try It/Solve It:

1. For this exercise you will have to update the JavaBank application to include an
option for the customer to choose their account type when they create an account.
This will include some changes to the GUI to accommodate the new features. Follow
the given instructions to update the bank application’s user interface (JavaBank.java).
a. Add a private JComboBox named accountTypes above the MAXACCOUNTS
statement. Also create a private field named actType of type AccountType that
will store the type of chosen account, this should be set to the default of savings
account.

3
Answer :

b. Under the code that sets up the displayJButton but before the code that sets up the
displayJLabel add the following code:

Answer :

c. Run the application, click proceed when the error message is displayed! You will
see that the combo box is not displayed properly as some of the interface
components require to be updated.
Answer :

4
d. Update the size of the window.
i. Go to the end of the createUserInterface method and identify the code that
sets the values for the application’s window.
ii. Update the size of the window from (670, 308) to increase the height to
340.
Answer :

e. Update the size of the panel that contains the interactive components.
i. Identify the code that sets up the inputDetailJPanel.
ii. Currently the bounds settings (X, Y, Width, Height) are (16, 16, 208, 250).
You need to increase the Height of the window by 30 pixels.
Answer :

5
f. Update the size of the text area to better suit the new size of the application
window.
i. Identify the code that sets up the displayJTextArea.
ii. Increase the Height of the window to a value of 245.

Answer :

2. The following task will update the required code to allow the user to select a bank
account from the drop-down list.
a. Update the myAccounts array declaration so that it instantiates
AbstractBankAccount objects and not Account objects. This is because you are
going to add both credit and savings accounts in the application. The Array must
be based on the superclass so that all of the subclasses can be stored in the array.

b. Update the if statement that adds accounts to the system (After the finally block in
the createAccountJButtonActionPerformed method) to include an if statement that
checks the account type.

3. The following tasks will add functionality to the bikeproject.


a. Create an Enum class named BikeUses that includes the following values:
off_road,
track,
road,
downhill,

6
trail
Answer :

b. Update the interfaces to use the enum for the terrain.


i. Update the MountainParts interface by commenting out the original line
that sets the terrain and replace it with the Enum declaration using the
off_road value.

ii. Do the same with the RoadParts interface using the Enum value track for
the terrain.

iii. Update the toString() method in both bike subclasses to also display the
terrain information so that the output matches:
Oracle Cycles This bike has Bull Horn handlebars on a Hardtail frame
with 27 gears. It has a dropper seat with Maxxis tyres. This mountain bike

7
is a Pro bike and has a RockShox XC32 suspension and a frame size of
19inches. This bike is best for off_road

iv. Run and test that the program displays the correct information.

4. You will now create a genericshapes project that will allow the use of a generic class.
a. Create a generic class called Cuboid that will store the three dimensions of a
cuboid. Add methods to set and get the length, breadth and Height. Add a method
public String toString() that will return all of the dimensions. The type of the
dimensions will be decided at construction of the cuboid instance. Example:
Cuboid c1 = new Cuboid<>();
Cuboid c1 = new Cuboid<>();

b. Create a driver class that will instantiate the Double cuboid with the values 1.3,
2.2 and 2.0.

8
c. Create a driver class that will instantiate the Integer cuboid with the values 1, 2
and 3.

d. Modify the generic class Cuboid so that it only accepts Numbers. Add a method
that returns the volume of the shape as a double (l*b*h)

e. In the driver class display the result of calling the getVolume() method on both the
c1 and c2 objects.

You might also like