0% found this document useful (0 votes)
32 views8 pages

DS Java 3A S1 2324 - EN

Uploaded by

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

DS Java 3A S1 2324 - EN

Uploaded by

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

Academic year : 2023 - 2024

Written DS

Semestre: 1 2

Module: Object oriented design and Java Programming Documents authorized : No


Internet authorized : No
Classes: 3A1, 3A2
Date: 08-11-2023
Duration: 1H
Time: 15H30
Number of pages: 7

Fullname: Class:

Exercice 1 (4 pts) :
Only one answer is correct
1. What will this code display, given that the "call" method throws an exception
of type "MyException" ?

PS: The MyException class inherits from the Exception class.

try{
System.out.print("AAA");
call();
System.out.print("BBB");
} catch(Exception ex) {
System.out.print("CCC");
} catch(MyException ex) {
System.out.print("DDD");
}

a. 🮕 AAACCC
b. 🮕 AAACCCDDD
c. 🮕 Compilation error
d. 🮕 AAABBBCCC

2. What is bytecode?

a. 🮕 Code written in byte form


b. 🮕 Code compiled in machine language
c. 🮕 Compiled code that can be executed by a virtual machine
d. 🮕 Source code

1
3. What will this code display?

public static void main(String[] args) throws ArithmeticException{

System.out.println(“Start”);
int x = 10 / 0;
System.out.println(“End”);

a. 🮕 The program displays "Start" then throws an exception


b. 🮕 Start End
c. 🮕 The program displays "Start End" and throws an exception
d. 🮕 Compilation error

4. In what situation is the “try-catch-finally” sequence used?

a. 🮕 To handle exceptions
b. 🮕 To exit the program
c. 🮕 To debug code
d. 🮕 To compile the program

Exercise 2(16 pts) :

As part of an academic project, you're tasked with developing a Java program that
implements a multimedia library, based on the class diagram provided and
following the instructions given (TODO) by your supervisor.

PS: Respect the Java Naming Rules (Conventions).

2
1. TODO 1 (1pt): The MultimediaFile class must allow inheritance only for the
Song and Movie classes.

2. TODO 2 (0.5pt): Specify the appropriate visibility level, given that the classes
are not in the same package

3. TODO 3 (0.5pt): Implement the parameterized constructor

4. TODO 4 (1pt): Implement the "toString()" method

5. TODO 5 (1pt): Create the "public int getSize()" method, knowing that it must be
redefined in the child classes.

Class MultimediaFile
//TODO 1
public ………………………………………class MultimediaFile ………………………………………………………………{

//TODO 2
…………………… int id;
…………………… String title;
…………………… int duration;
…………………… int bitrate;

//TODO 3
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………

//TODO 4
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
…………………………………………

//TODO 5
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………
………………………………………………………………………………………………………………………………………………………………………
……

3
}

6. TODO 6 (0.5pt): The Film class can be extended

7. TODO 7 (1pt): Implement the parameterized constructor

8. TODO 8 (1pt): Implement getter and setter for the attribute releaseDate

9. TODO 9 (1pt): Redefine the "public int getSize()" method, knowing that if it's a
short film, the formula is duration * bitrate / 2 and duration * bitrate otherwise.

Class Film
//TODO 6

public …………………………………… class Film ……………………………………………………………………………{

private int releaseDate;


private String type; //represents whether the film is a short or long film

//TODO 7
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
………………………………………………………

//TODO 8
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………

//TODO 9
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………

4
……………………………………………………………………………………………………………………………………………………………………
………………………………………………
}

10. TODO 10 (0.5pt): The Song class cannot be extended

11. TODO 11 (1.5pts): Redefine the "equals()" method, knowing that two songs are
identical according to their title, extension and identifier.

Class Song

//TODO 10
public ……………………………… class Song………………………………………………………………………… {

private String extension;

/*
Constructor, getters, setters and getSize() method already implemented
*/

//TODO 11
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………
………
……………………………………………………………………………………………………………………………………………………………………
………
……………………………………………………………………………………………………………………………………………………………………
………

public void addToFavorites(){


System.out.print("The song"+ title + " is added to favorites”);
}

12. TODO 12 (1pt): Add the instruction needed to create an ensemble of


multimedia files and complete the declaration of the MAX_CAPACITY variable.

5
13. TODO 13 (2pts): The "addSong(MultimediaFile f)" method allows you to add
only songs and increment the total size of the playlist. If another media type is
supplied, the method triggers an UnsupportedFileException with the message
"This file type is not compatible".

PS: The UnsupportedFileException class has already been created.

14. TODO 14 (2pt): The "public void sortSongs()" method sorts songs in
ascending order of size.

Classe Playlist
public class Playlist {

private int nbrSongs;


private float totalSize;
//TODO 12
private ………………………… int MAX_CAPACITY = 20; // represents the maximum number of files
…………………………………………………………………………………………………………………………………

/*
Constructor, getters and setters methods already implemented
*/

//TODO 13
public void addSong(MultimediaFile f) …………………………………………………………………………………{
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………
}

//TODO 14
public void sortSongs(){
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………

6
………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
……………………………………
………………………………………………………………………………………………………………………………………………………………………
…}

15. TODO 15 (1.5pts): Here's a "main" method implemented by a novice engineer.


Your task is to identify errors in his code and correct them.

Méthode Main
public static void main(String[] args) throw NullPointerException{

Playlist playlist = new Playlist();

MultimediaFile fm = new Song(12, "Night Wind", 223, 1400, "mp3");

playlist.addSong(fm);

fm.addToFavorites();
}
//TODO 15
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………

7
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………
……

You might also like