Chapter 4 – Arrays & Strings
1. In this
exercise you will fix several java
applications by solving compilation and run time errors.
-
public class HelloLoop1
{
Math
math = new Math();
public
static int doTheCalculatingStuff(float num)
{
return
math.sqrt(num);
}
public
static void main(String args[])
{
float
vec[10];
for(int
index = 0; index < 10; index = index + 1)
{
vec[index]
= doTheCalculatingStuff(0);
System.out.println(vec[index]);
}
}
}
-
public class HelloLoop2
{
int
id;
public
static void main(String args[])
{
HelloLoop
vec[];
vec
= new HelloLoop[10];
for(int
index = 0; index < 10; index++)
{
vec.id
= index;
}
for(int
number=0; number < 10; number++)
{
System.out.println(vec[number].id);
}
}
}
[solution]
2.
Write an application that stores the integer numbers between
1 (included) and 64 (included) in an array and prints them in a reverse order.
[solution]
3.
Write an application that creates a two dimensions array 10
X 10 and stores the multiplication table in it. The application should also
prints the multiplication table values.
[solution]
4. Write an application that stores the numbers of the
first 10 rows in the Pascal Triangle in a two-dimension array in which the number
of rows is 10 and the number of columns is different in every row.
[solution]
5. Write an
application that stores the factorials of all the numbers between 1 (included)
and 10 (included) in a vector and prints them in a reverse order.
[solution]
6.
Given the following source code, add the needed statements
in order to print the numbers in their order.
public class NumbersOrder
{
public
static void main(String args[])
{
int
vec[] = {1,2,3,65,23,78,5,2,1,234,23,21,2,23,323,1111,21};
//add
your code here
for(int
index=0; index<vec.length; index++)
{
System.out.println(vec[index]);
}
}
}
[solution]
7.
Given the following source code, add the needed statements
in order to print the strings in their lexicographic order.
public class StringsOrder
{
public
static void main(String args[])
{
String
vec[] = {“abc”, “aab”, “aaa”, “zxc”, “cxs”, “qwe”, “gfd”, “hjk”, “asd”};
for(int
index=0; index<vec.length; index++)
{
System.out.println(vec[index]);
}
}
}
[solution]
8.
Given the following source code, add the needed statements
and declarations in order to get the details of the students printed to the
screen according to their average.
public class StringsOrder
{
public
static void main(String args[])
{
Student
vec[] = {new Student(“Danidin”,88), new Student(“Avy”,94), new Student(“Ronen”,
84), new Student(“Ramy”, 78)};
sort(vec);
print(vec);
}
public
static void sort(Student vector[])
{
//add
your code here
}
public
static void print(Student vector[])
{
for(int
i=0; i<vector.length; i++)
{
System.out.println(vector[i]);
}
}
}
class Student
{
//add
your code here
}
[solution]
9.
Declare the following classes: ClassRoom, Teacher and
Student.
The ClassRoom class should have the following private attributes:
-
teacher: Teacher
-
students: []Student
-
currentNumberOfStudents: int
The ClassRoom class should have the following public
methods:
+
ClassRoom(int)
This constructor instantiate the new instance of type ClassRoom as a one that
the maximum number of students it can have is the value this constructor
receives.
+
addStudent(Student): boolean
This method should add another student to the class and return a boolean value
which means whether the student adding has succeeded.
+
removeStudent(int): boolean
+
getStudent(int): Student
+
getAverage(): double
This method should return the students average
+
toString(): String
This method should make a use of the StringBuffer class
The Student class should have the following private
attributes:
-
privateName: String
-
familyName: String
-
age: double
-
average: double
The Studetn class should have the following public methods
+
Student(String, String, double, double)
+
getAverage(): double
+
setAverage(double): void
+
toString(): String
You can add more attributes and methods as needed. The class
Teacher should have attributes and methods by your design.
You should check these classes with an other class that acts
as an application that creates a new instance of type ClassRoom, invokes
different methods on it and send
it to the screen using a System.out.println statement.
[solution]
10.
Declare the Library and Book classes to describe a library
and a book. One of the attributes that the Library class should have is an
array of books. You should design and declare the needed attributes and
methods. You should check these classes in a separate class that acts as a
stand-alone application.
[solution]
11.
Declare the Trip class that represents a trip to another
country\countries. The class should have the appropriate attributes and
methods. One of the class attributes is an array of type Destination (you
should declare this class too) that holds references of objects that represents
the places in which this trip includes. Check the class you declared.
[solution]
12.
Declare the Interview class that represents an interview.
You should declare the needed attributes and methods and check them. One of the
attributes should be an array of type String that holds the names of the
countries in which the employee worked.
[solution]
13.
Create an application that receives through the command line
unknown number of different numbers and prints them back to the screen.
[solution]
14.
Create an application that receives through the command line
unknown number of different numbers, sorts them and print them (sorted) back to
the screen.
[solution]
15.
Create an application that receives through the command line
unknown number of different numbers and prints their sum back to the screen.
[solution]
16.
Create an application that receives through the command line
5 different names and prints them (sorted) back to the screen.
[solution]
17.
Create an application that receives through the command line
a number in base 10 and prints its equivalent in base 2.
[solution]
18.
Create an application that receives through the command line
a number in base 2 and prints its equivalent in base 10.
[solution]
19.
Create an application that receives through the command line
a number in base 10 and the base to which the application will translate the
given number and prints its result.
[solution]
20.
Declare a class that represents an array of doubles.
The class should have one attribute, which is a variable that holds a reference
to an array of doubles.
The class should have the following methods & constructors:
-
A constructor that receives one argument (int) and
instantiate a new array (of doubles).
-
A constructor that receives two arguments. The first is the
size of the array and the second is the value that each of the array cells will
be initialize with.
-
The method get(int) that returns the value in a specific
place of the array.
-
The method set(double,int) that receives two arguments, the
value and the desired index location, and place the value in the desired
location.
-
The method getAverage() that returns the average of the
numbers that the array holds.
-
The method toString() that returns a textual representation
of the object on which it was invoked.
You should check this class by instantiating it and invoke
the different methods.
[solution]
21. Declare the
MobileTelephone class to describe a mobile telephone. The class should have the
following private attributes:
ownerName : Owner
color:
java.awt.Color
number : long
model :
java.lang.String
The MobileTelephone class should have
the following public methods:
+
setOwner(Owner):void
+
getOwner():Owner
+
setColor(java.awt.Color):void
+
getColor():java.awt.Color
+
setNumber(long):void
+
getNumber():long
+
setModel(java.lang.String):void
+
getModel():java.lang.String
+
toString():java.lang.String
The Owner
class that describes a mobile telephone owner should have the following private
attributes:
+ MAX_NUM_OF_MOBILES : final int
-
name : java.lang.String
-
id : long
-
mobileTelephone :
MobileTelephone[]
-
birthday :
java.util.Date
The Owner class should have the
following public methods:
+
setName(java.lang.String):void
+
setId(long):void
+
setBirthdat(java.util.Date):void
+
toString():java.lang.String
+
addtMobileTelephone(MobileTelephone):void
The toString()
method in class Owner should also print the details of all the mobile telephone
that belongs to the owner on which the method was invoked. You can add more
constructors and methods to each one of the classes. You should check the
classes in a separate class that acts as a stand-alone application, which
creates several MobileTelephone object and connects them to an Owner object and
then invokes the toString() method on the Owner object.
[solution]
22. Declare the
Party class to describe a party that you organize. The class should have the
following private attributes:
-
place:java.lang.String
-
date:java.util.Date
-
dj:Person
-
guests:Person[]
The Party class should also have the
following public methods:
+
Party(String,java.util.Date,Person,Person[])
+
setPlace(java.lang.String):void
+
setDate(java.util.Date):void
+
setDJ(Person):void
+
setGuests(Person[]):void
+
getPlace():java.lang.String
+
getDate():java.util.Date
+
getDJ():Person
+
getGuests():Person[]
+
toString():java.lang.String
The Person
class that describes a person should have the following private attributes:
-
name : java.lang.String
-
id : long
-
birthday :
java.util.Date
The Person class should have the
following public methods:
+
setName(java.lang.String):void
+
setId(long):void
+
setBirthdat(java.util.Date):void
+
getName():java.util.String
+
getId():long
+
getBirthday():java.util.Date
+
toString():java.lang.String
You can add
more constructors and methods to each one of the classes. You should check the
classes in a separate class that acts as a stand-alone application, which
instantiate these classes and check the new created objects by invoking the
toString() method.
[solution]
23. Declare the
Book class to describe a book in the library. The class should have the
following private attributes:
-
title:java.lang.String
-
author:Author
-
pages:int
-
price:double
The Book class should also have the following
public methods:
+
setTitle(java.lang.String):void
+
setAuthor(Author):void
+
setPages(int):void
+
setPrice(double):void
+
getTitle():java.lang.String
+
getAuthor():Person
+
getPages():int
+
getPrice():double
+
toString():java.lang.String
The Author
class that describes the author should have the following private attributes:
-
name:java.lang.String
-
books:Book[]
-
id:long
The Author class should have the
following public methods:
+
Author(java.util.String,Book[],long,java.util.Date)
+
setName(java.lang.String):void
+
setId(long):void
+
setBooks(Book[]):void
+
getName():java.util.String
+
getId():long
+
getBooks():Book[]
+
toString():java.lang.String
You can add
more constructors and methods to both of the classes. You should check the
classes in a separate class that acts as a stand-alone application, which
instantiate the class Author in order to represent an author that wrote several
books. Later, the stand-alone application instantiate the class Book to
represent the books that were wrote by that author. Then, the toString() method
is invoked on each one of the objects.
[solution]
24. Declare the Trip
class that describes a trip to more than one country. The class should have the
following private attributes:
-
length:int
-
places:String[]
The Vacation class should also have the
following public methods:
+
setPlaces(java.lang.String[]):void
+
setLength(int):void
+
getPlaces():String[]
+
getLength():int
+
toString():java.lang.String
You can add more
constructors and methods to both of the classes. You should check the class in
a separate class that acts as a stand-alone application, which instantiate the
class and check the new object by invoking the toString() method.
[solution]
25. Declare the Hotel
class that describes a hotel. The class should have the following private
attributes:
-
rooms:Room[]
-
place:String
The Hotel class should have the
following public methods:
+
Hotel(Room[],String)
+
setRooms(Room[]):void
+
setPlace(java.lang.String):void
+
getRooms():Room[]
+
getPlace():String
+
toString():java.lang.String
The Room class
should have the following private attributes:
-
beds:int
-
tv:boolean
-
telephone:boolean
The Room class should have the
following public methods:
+
Room(int,boolean,boolean)
+
setBeds(int)
+
setTV(boolean)
+
setTelephone(boolean)
+
getBeds():int
+
getTV():boolean
+
getTelephone():boolean
You can add
more constructors and methods to both of the classes. You should check the
Hotel class in a separate class that acts as a stand-alone application, which instantiate
the Hotel and Room classes and check the new objects by invoking the toString()
method on each one of the new objects.
[solution]
26. Declare the Hotel
class that describes a hotel. The class should have the following private
attributes:
-
rooms:Room[]
-
name:java.lang.String
-
place:Place
The Hotel class should have the
following public methods and constructors:
+
Hotel(Room[],java.lang.String,
Place)
+
setRooms(Room[]):void
+
setName(java.lang.String):void
+
getRooms():Room[]
+
getName():java.lang.String
+
setPlace(Place):void
+
getPlace():Place
+
toString():java.lang.String
The Place
class should have the following private attributes:
-
cityName.java.lang.String
-
countryName:java.lang.String
-
currency:Currency
-
cityHallTel:java.lang.String
The Place
class should have the following public methods and constructors:
+
Place(java.lang.String,
java.lang.String, Currency, java.lang.String)
+
getCityName():java.lang.String
+
setCityName(java.lang.String):void
+
getCountryName():java.lang.String
+
setCountryName(java.lang.String):void
+
getCurrency():Currency
+
setCurrency(Currency):void
+
getCityHallTel():java.lang.String
+
setCityHallTel(java.lang.String):void
The Currency
class should have the following private attributes:
-
name:java.lang.String
-
currentDollarRate:double
The Currency class
should have the following methods and constructors:
+
Currency(java.lang.String,
double)
+
setName(java.lang.String):void
+
getName():void
+
setCurrentDollarRate(double):void
+
getCurrentDollarRate():void
+
toString():java.lang.String
The Room class
should have the following private attributes:
-
beds:int
-
tv:boolean
-
telephone:boolean
The Room class should have the
following public methods:
+
Room(int,boolean,boolean)
+
setBeds(int)
+
setTV(boolean)
+
setTelephone(boolean)
+
getBeds():int
+
getTV():boolean
+
getTelephone():boolean
You can add
more constructors and methods to both of the classes. You should check these
classes by creating a stand-alone application that describe the hotels in Eilat
and Tiberius. You should check the new objects by invoking the toString()
method on them.
[solution]
27. Declare the Week
class that describes one week. The class should have the following attribute:
-
days:Day[]
The Day class should have the following
attribute:
- hours:Hour[]
The Hour class
should have the following attribute:
- minutes:Minute[]
The Minute
class should have the following attribute:
-
seconds:Second[]
Add more
attributes, methods and constructors as needed to each of these classes and
test them by instantiating the Week class.
[solution]
28. Declare the Hand
class that describes a human hand. The class should have the following attribute:
-
fingers:Finger[]
The Finger class should have the following attribute:
-
length:double
Add more attributes, methods and constructors as needed to each of these
classes and test them by instantiating the Hand class.
[solution]
29. Declare the
Screen class that describes a video screen. The class should have the following
attribute:
- pixels:Pixel[][]
The Screen class should have the following constructor:
+ Screen(int width, int height)
The Pixel class should have the following attribute:
- color:java.awt.Color
Add more attributes, methods and constructors as needed to each of these
classes and test the classes by instantiating the Screen class.
[solution]
30. Declare the Train
class that describes a train. The class should have the following attributes:
- locomotive : Locomotive
- wagons : Wagon[]
The Train class should have the following methods:
+ toString() :
java.lang.String
The Locomotive class that describes a locomotive should have the following
attributes:
- driver : Person
- engine :
Engine
The Wagon class that describes a Wagon should have the following attributes:
- sits : Chair[]
- doors : Door[]
The Person class that describes a person should have the following attributes:
- name : java.lang.String
The Chair class that describes a chair should have the following attributes:
- number
: int
- nearWindow :
boolean
The Door class that describes a door should have the following attributes:
- height : double
- width : double
Add more attributes, methods and constructors as needed to each of these
classes and test them by instantiating the Train class.
[solution]
For your convenience, hereto the links to all of the chapters.
Chapter 1: Let’s Start
Chapter 2: Basic
Chapter 3: Classes
Chapter 4: Arrays & Strings
Chapter 5: Inheritance
Chapter 6: Reflection
Chapter 7: Inner Classes
Chapter 8: Applets
Chapter 9: Threads
Chapter 10: GUI using AWT
Chapter 11: Events Handling
Chapter 12: Exceptions Handling
Chapter 13: I\O Streams
Chapter 14: Networking
Chapter 15: GUI using Swing
Chapter 16: Collections
Chapter 17: JDBC
Chapter 18: RMI
Chapter 19: Data Structures
Chapter 20: Internationalization
Chapter 21: JNI
Chapter 22: 2D API
Chapter 23: Java Security
Chapter 24: JMF
Chapter 25: Memory Management
Chapter 26: Java & UML
Chapter 27: Java & Design Patterns
Chapter 28: Java & CORBA
Chapter 29: Java Performance
Chapter 30: Java Beans
2000 © All the rights reserved to
Haim Michael & Zindell Publishing House Ltd.
No
parts of the contents of this paper may be reproduced or transmitted
in
any form by any means without the written permission of the publisher !
This
book can be used for personal use only !!!
Brought
to you by ZINDELL