0% found this document useful (0 votes)
2 views6 pages

CS342 Lab1

The document outlines a series of exercises for a computer science lab focusing on object-oriented programming concepts. It includes the creation of various classes such as Person, Customer, Owner, Film, and geometric shapes, along with methods for displaying and managing their attributes. Additionally, it covers exception handling and the implementation of interfaces in Java, along with testing procedures for the created classes.

Uploaded by

rr1q1234
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)
2 views6 pages

CS342 Lab1

The document outlines a series of exercises for a computer science lab focusing on object-oriented programming concepts. It includes the creation of various classes such as Person, Customer, Owner, Film, and geometric shapes, along with methods for displaying and managing their attributes. Additionally, it covers exception handling and the implementation of interfaces in Java, along with testing procedures for the created classes.

Uploaded by

rr1q1234
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/ 6

Kingdom of Saudi Arabia ‫المملكة العربية السعودية‬

Ministry of Education ‫وزارة التعليم‬


College of computer ‫كلية الحاسب‬
Department of Computer Science ‫قسم علوم الحاسب‬

LAB1: Basic Concepts


Exercice 1:

1) Define a Person class containing two private attributes: lastname, firstname. Provide
this class with a constructor allowing the initialization of its attributes and a method that
displays the last name and first name of a person: void displaysPerson ().
2) Define a Customer class inheriting from the Person class and having a private
attribute: numidentity. Provide this class with a constructor allowing you to create a
customer from their last name, first name and identity card number and a method:
• void displaysCustomer () which displays the lastname, firstname and
numidentity of a customer.
3) Define a Owner class inheriting from the Person class and having two private
attributes: name of the video club (nameclub), address of the club (adrclub). Provide
this class with a constructor allowing to create a landlord from his name, his first name,
the name of his video club, the address of his club and a method:
void displaysOwner() who displays the name, first name, last name and address of his
video club.
4) Define a Date class containing three private attributes day, month, year. Provide this
class with a constructor allowing the initialization of its attributes.
5) Define a Film class such that any film is defined by its title, genre, identifier and date
of first transmission (datetrans). Provide this class with a constructor allowing the
initialization of its attributes and a method (void displayFilm ()) which displays the
characteristics of a movie.
6) Define an Organize class which contains private attributes: tabfilm which will be an
array of films, nbfilm (indicates the number of films in the tabfilm array) and maxfilm
(indicates the size of the array).
This class will contain methods for.
• Add films: void add (Film f) the film is added at the end of the table (tabfilm), if
the table is full it displays an error message. Display the list of films: void
displaysListFilm ().

1
• Search for a movie by its title: void searchesMovie (String title) and displays its
characteristics, otherwise it displays an error message.
7) Create a Test class in order to test all the created classes.

Exercice 2:

Let the abstract class Person. It is characterized by a protected attribute coordinates of


the Coordinates type indicating the coordinates of a person, and by an integer nbPerson
indicating the number of people. This class has as services an abstract method
writePerson (), allowing to display the coordinates of a person, and a method
ModifyAddress (String addr), allowing to modify the address of this person and to
display its characteristics.
Let the Secretary class extend the Person class and have two private attributes String
officeNumber and an integer nbSecretary, indicating the number of secretaries.
Let the Teacher class extend the Person class and have two private attributes String
specialite, indicating the teacher's specialty, and an integer nbTeacher indicating the
number of teachers.
Modify the Coordinates class by adding the public method setAddress (String adr)
which allows you to modify the address of this class.
Note that all the classes respect the hierarchy in the figure below.

Exercice 3:

It is assumed that each geometric shape is characterized by a perimeter calculation


method and an area calculation method. These two methods return a result of type
double and do not take any parameters.
A circle is a geometric shape characterized by a center (of type Point) and a radius (of
type double). You can modify the radius and return the radius value.
A rectangle is a geometric shape characterized by a length (double type) and a breadth
(double type). You can change the length as you can change the width.

2
A point is characterized by its abscissa (of double type) and its ordinate (of double
type).
Questions:
1) Define the Point, Shape, Circle and Rectangle classes
2) Write a small application as a TestForm class that creates an object of each
geometric shape and displays their area and perimeter.

Exercice 4:

We ask to repeat the exercise 2, assuming that Shape is an interface. We also assume
that the Circle class has a constructor with the following prototype:
public Circle (double, Point)
Questions:
1) Define the Shape interface
2) Define a Displayable interface including a void display () method
3) Define the Point class which implements the Displayable interface
4) Define the Circle and Rectangle classes which implement Shape and Displayable
5) Write a small application in the form of a TestFormInterface class which allows you
to declare an array (TabObjet []) of 3 Displayable type inputs. To create: a Point object
and put its reference in the first entry, a Rectangle object and put its reference in the
second entry and a Circle object and put its reference in the third entry. The circle object
uses the point of the first entry in the array as the center. Display the characteristics of
all the objects that are in the table.

Exercice 4:

1) Define a Displayable interface containing a void displays () method.


2) Define a Document class containing three private attributes: String author, int
numIdentification, int nbPage. Provide this class with a constructor allowing the
construction of a Document type object characterized by these attributes. You can return
the document identification number. This class implements the Displayable interface.
3) A book is a Document characterized by its publisher. This class implements the
Displayable interface.
4) Define a Shelf class which contains three attributes: tabDoc which will be an array of
documents, nbDoc (indicates the number of documents in the tabDoc array) and
maxDoc (indicates the size of array 50). This class implements the Displayable interface
and contains a search () method that allows you to search for a document by its
identification number.

3
5) Add the TestShelf class which allows you to test all the classes.

Exercice 5:

Write the following program, compile it and run it.

public class Exemple1 {


public static void main(String[] args) {
int x = 10, y = 2;
int z = x / y;
System.out.println(x + "/ " + y + " = " + z);
System.out.println("Program End");
}
}

Modify the program by initializing y at 0. Recompile and re-run the program. What’s
going on? Did the program end well? Consider a solution to this problem:
1. Using a predefined exception class.
2. Using a predefined exception class with a custom message.
3. Using your own exception class (give a meaningful name and message).

Exercice 6:

What does the CalculMoyennelanced program give in the following three cases:
• String tab[] = {"14", "12", "13", "16", "18", "15"};
• String tab[] = {"x", "2", "-1", "30", "18"};
• String tab[] = {"x", "-3", "2.3"};

What are the predefined exceptions captured by this program?


What are the exceptions created by the programmer?

public class CalculMoyenne{


private static int moyenne(String [] liste)throws
ExceptionRien{
int sum = 0, integer, nbNotes = 0;
for(int i=0; i<list.length; i++){
try{
entier = Integer.parseInt(liste[i]);
if(integer<0)
throw new ExceptionIntervalle("petite");
else if(all>20)
throw new ExceptionIntervalle("grande");
else{
sum += integer
4
nbNotes++;
}
}
catch(NumberFormatException nfe){
System.out.println("Note no."+(i+1)+" is not
complete");
}
catch(ExceptionIntervalle ei){
System.out.println("Note no."+(i+1)+" is too
"+ei.getMessage());
}
}//Fin for

if(nbNotes == 0)
throw new ExceptionRien();
return (int)sum/nbNotes;
}

public static void main(String [] args){


String tab[] = {……………………………… };
try{
System.out.println("The average is: "+average(tab));
}
catch(exceptionRien er){
System.out.println(er);
}
}
}
public class ExceptionRien extends Exception{
public String toString(){
return("No note is valid");
}
}

public class ExceptionIntervalle extends Exception{


public ExceptionIntervalle(String s){
super(s);
}
}

Exercice 7:

Either the Next Time class :

public class Temps{


private int hours;
private int minutes;
private int seconds;

public Temps(int h, int m, int s){


hours = h
minutes = m;
seconds = s;
}

public static void main(String [] args){


5
Time t = new Time(24, 12, 67);
}
}

1. Modify the constructor of this class so that it throws a TempsException(to create)


exception if hours, minutes or seconds do not match a valid time.
Change method code mainto the TempsExceptionbe processed by displaying the
message " Invalid time".

You might also like