Al-Baha University ﺟ ﺎﻣ ﻌ ﺔ اﻟﺒﺎﺣ ﺔ
Faculty of Computing and Information ﻛ ﻠﯿﺔ اﻟﺤ ﺎﺳ ﺒ ﺎت و اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت
Computer Information Systems Program ﺑﺮ ﻧﺎﻣ ﺞ ﻧﻈ ﻢ اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت اﻟﺤ ﺎﺳ ﻮ ﺑﯿﺔ
Programming 2
Assignment (2)
2025
(10 Marks)
Instructions:
• The purpose of this assignment is to assess student in CLO 1.2 and CLO 2.1.
• There are four questions in the assignment, and you must answer them all.
• The answers should be your own.
• Use (Rafid) to submit YOUR Assignment in a PDF file.
• The deadline for submitting your assignment through (Rafid) is 1
11:59 pm (Midnight), Sunday,October 26, 2025
• When submitting work, it is your responsibility to ensure that all work submitted is
• Entirely your own work
• On time
• Marks will be awarded on the basis of the following scheme:
• Question 1 (2 %)
• Question 2 (2 %)
• Question 3 (2 %)
• Question 4 (2 %)
• Question 5 (2 %)
Please note that there are severe penalties for submitting work which is not your own.
1
Al-Baha University ﺟ ﺎﻣ ﻌ ﺔ اﻟﺒﺎﺣ ﺔ
Faculty of Computing and Information ﻛ ﻠﯿﺔ اﻟﺤ ﺎﺳ ﺒ ﺎت و اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت
Computer Information Systems Program ﺑﺮ ﻧﺎﻣ ﺞ ﻧﻈ ﻢ اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت اﻟﺤ ﺎﺳ ﻮ ﺑﯿﺔ
Question 1:
Given the code below, what is the expected output. [2 Marks]
public abstract class Animal {
public void typeAnimal(){
System.out.println("What type an animal is?");
}
}
public interface Drawable {
public abstract void draw();
}
public class Cat extends Animal {
public void typeAnimal(){
System.out.println("Cat is friendly");
}
}
public class Fish implements Drawable {
public void draw(){
System.out.println("Easy to draw fish");
}
}
public class Test{
public static void main(String[] args){
Animal a = new Cat();
a.typeAnimal();
Fish f = new Fish();
f.draw();
}
}
Output:
2
Al-Baha University ﺟ ﺎﻣ ﻌ ﺔ اﻟﺒﺎﺣ ﺔ
Faculty of Computing and Information ﻛ ﻠﯿﺔ اﻟﺤ ﺎﺳ ﺒ ﺎت و اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت
Computer Information Systems Program ﺑﺮ ﻧﺎﻣ ﺞ ﻧﻈ ﻢ اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت اﻟﺤ ﺎﺳ ﻮ ﺑﯿﺔ
2.
public abstract class Food {
public void type(){
System.out.println("This describes the type of food ");
}
}
public class Fruit extends Food {
public void type(){
System.out.println("This is Fruit ");
}
public void quantity(){
System.out.println("The number of Fruit is 5 ");
}
}
public class MainTest {
public static void main(String[ ] args) {
Food food1 = new Fruit ();
food1.type ();
Fruit food2 = new Fruit ();
food2. quantity ();
}
}
Output:
Question 2:
Point out the error in the code and correct it. [2 Marks]
Java Code
public class Program{
}
public interface College {
}
public class Student extends College implements Program {
private int id;
}
3
Al-Baha University ﺟ ﺎﻣ ﻌ ﺔ اﻟﺒﺎﺣ ﺔ
Faculty of Computing and Information ﻛ ﻠﯿﺔ اﻟﺤ ﺎﺳ ﺒ ﺎت و اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت
Computer Information Systems Program ﺑﺮ ﻧﺎﻣ ﺞ ﻧﻈ ﻢ اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت اﻟﺤ ﺎﺳ ﻮ ﺑﯿﺔ
Errors and corrections:
Error:
Correct:
Java Code
public class Student {
String name;
int id;
public Student(){
}
public Student(String name, int id){
this.name=name;
this.id=id;
}
}
public class Undergards extends Student{
public Undergards(){
super(4443032, " Amal " );
}
}
Errors and corrections:
Error:
Correct:
Question 3:
Answer the following: [2 Marks]
* What interface represents a collection that does not allow duplicate elements?
................................................................................................................................................
* What are the differences between HashMap and ArrayList?
................................................................................................................................................
................................................................................................................................................
................................................................................................................................................
4
Al-Baha University ﺟ ﺎﻣ ﻌ ﺔ اﻟﺒﺎﺣ ﺔ
Faculty of Computing and Information ﻛ ﻠﯿﺔ اﻟﺤ ﺎﺳ ﺒ ﺎت و اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت
Computer Information Systems Program ﺑﺮ ﻧﺎﻣ ﺞ ﻧﻈ ﻢ اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت اﻟﺤ ﺎﺳ ﻮ ﺑﯿﺔ
Question 4:
Convert the schema of the UML class diagram to java code. [2 Marks]
Answer:
2.
<<interface>>
Type
+ getType (): String
<<interface>>
Food
Machine
+ model (): String # getName (): String
5
Al-Baha University ﺟ ﺎﻣ ﻌ ﺔ اﻟﺒﺎﺣ ﺔ
Faculty of Computing and Information ﻛ ﻠﯿﺔ اﻟﺤ ﺎﺳ ﺒ ﺎت و اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت
Computer Information Systems Program ﺑﺮ ﻧﺎﻣ ﺞ ﻧﻈ ﻢ اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت اﻟﺤ ﺎﺳ ﻮ ﺑﯿﺔ
Answer:
Question 5:
Consider the following Linked List - employeeList - [2 Marks]
head
Ali Sara Lama Osama Leen null
6
Al-Baha University ﺟ ﺎﻣ ﻌ ﺔ اﻟﺒﺎﺣ ﺔ
Faculty of Computing and Information ﻛ ﻠﯿﺔ اﻟﺤ ﺎﺳ ﺒ ﺎت و اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت
Computer Information Systems Program ﺑﺮ ﻧﺎﻣ ﺞ ﻧﻈ ﻢ اﻟﻤ ﻌ ﻠﻮ ﻣ ﺎت اﻟﺤ ﺎﺳ ﻮ ﺑﯿﺔ
Write a java program to:
1- Create a linked list of String type
2- Add the above list into the linked list of strings
3- Add an iterator to this list
4- Remove the last element
5- Print the elements of list
import java.util.LinkedList;
import java.util.ListIterator;
public class LLTest {
public static void main(String args[]) {
// create a linked List of strings
.............................................................
.............................................................
//add the created list into a linked list of strings
employeeList.add("Ali");
.............................................................
.............................................................
.............................................................
.............................................................
//add an iterator to this list
.............................................................
.............................................................
// move to the last element in the list
while(iterator.hasNext()) {
iterator.next();
}
//remove the last element
.............................................................
.............................................................
// print the elements of the list
.............................................................
.............................................................
.............................................................
}
}
*** THE END ***