You are on page 1of 11

‫جامعة ام القرى‬

‫كلية الحاسب االلي بالقنفذة – قسم علوم الحاسوب‬


Name: Shoroog Abdulla Al-Jedaani No: 437008550

Take Home Exam (Spring 2019/2020)


Advanced Programming
Date: 13 / 09 /1441 Duration: 6 Days
To be submitted on 02:00 pm of 20th Ramadan 1441

Instructions to Students: ‫تعليمات للطالب‬


The answer sheet should be word file (docx) any long.
.docx ‫يجب أن تكون ورقة الحل في ملف نصي ورد من نوع‬
 References in the text may either be indicated by the author’s name and year of the publication in
parentheses or numbers in parentheses; however, the chosen method must be used consistently.
‫يمكن( اإلشارة إلى المراجع في النص باسم المؤلف وسنة النشر بين قوسين أو أرقام بين قوسين ؛‬

 The final task should be word-processed. No handwritten assignment will be accepted.


‫يجب ان يكون الحل بداخل الملف النصي مطبوع طباعة ال يحتوي على صور نهائيا‬.
 Text should be typed on one side of the A4 size paper, justified and in single-line spacing with ‘Time
New Roman’ font type of 12 point. The top and bottom margins should be 1”while the left and right
margins should be set to 1.25”.
‫ و حجم‬Time New Roman ‫ و المحاذاة من االطراف متساوية و المسافة بين االسطر سطر واحد ون((وع الخ(ط‬A4 ‫حجم الصفحة‬
”1.25 ‫" و السيار و اليمين‬1 ‫ و المسافات في اعلى و اسفل الصفحة‬12
 All pages should be consecutively numbered. Page numbers should be positioned at the bottom-
center of each page.
.‫كل الصفحات يجب ان تكون مرقمة تسلسي‬
 Proof reading for spelling and grammar errors before submission. Clarity of expression and clear
layout is taken into account when awarding marks.
.‫تاكد من الكلمات بشكل لغوي و قواعدي قبل التسليم الن ذلك سياخذ بالحسبان في التقييم‬

 To avoid plagiarism, acknowledge the source and provide the full citation of each reference used,
including URL. ‫يجب ذكر المصادر التي استلت منه((ا المعلوم((ات ح((تى يك((ون البحث غ((ير مقتبس و نحاف((ظ على حق((وق الملكي((ة‬
‫لالخرين‬
 Heavy penalty would be imposed for:
 Late submission of final task ‫اي تاخير في التسليم سيكون عليه حسم درجات‬
 Non-compliance to the instructions given ‫ال يمكن االعتراض بعد التسليم ال يمكن( رفعه(ا( ب(اي طريق(ة‬
‫اخرى‬
Final Task Submission

Completed final task should be submitted to blackboard website Folder Named (Final Exam
Submission).

‫ المس((مى (رف((ع االختب(ار‬blackboard ‫يجب رفع الملف النصي بعد االنتهاء( و التاكد من صحة المععلومات بالملف إلى مجلد موقع ويب‬
)‫النهائي‬

1
Q1: (10 marks) Write Java desktop application that satisfy the following:

:‫أكتب برنامج بلغة الجافا ينطبق عليه الشروط التالية‬

1- Create your own class depend on your previous project.


2- Write and read your class objects from a binary file.
3- Create your own menu in where user can choose (Add new
object, Delete, Update, Search by, Print list of object, Exit)
4- Use at least one exception in your code.

(copy your code from IDE with minimum space interval)

.‫انسخ الكود هنا باقل المسافات بين السطورمع المحافظة على المسافة البادئة‬

import java.lang.*;
import java.util.*;
import java.io.*;
import java.io.ObjectInputStream;

/**
*
* @author Shoroog Abdulla Al-Jedaani
*/

class car
{
int car_id;
String car_name ;
String car_type ;
int car_price ;
int car_model;
int count = 1;

java.util.List<String> carList = new java.util.ArrayList<String>();


//

car(){
System.out.println("");
}

public void car_add(){


Scanner input = new Scanner(System.in);

if (carList.isEmpty()){

2
int i = 1;
car_id =1;
String car_no = Integer.toString(i);
carList.add(car_no);}

else{

//String p_index = carList.get(count);


String car_no = Integer.toString(car_id);
carList.add(car_no);
//System.out.println(p_index + " is ");
// int pro_index = Integer.parseInt(p_index);
}

System.out.println("Enter car name :- ");


car_name = input.nextLine();
carList.add(car_name);
System.out.println("Enter " + car_name + " Type :- ");
car_type = input.nextLine();
carList.add(car_type);
System.out.println("Enter " + car_name + " Price :- ");
car_price = input.nextInt();
String car_cost = Integer.toString(car_price);
carList.add( car_cost);
System.out.println("Enter " + car_name + " model :- ");
car_model = input.nextInt();
String car_amount = Integer.toString(car_model);
carList.add(car_amount);
count += 5;
car_id++;
car_Display();
//return 0;
}
public void car_print(){

String item ;
try{
for(int i=0 ; i<count-1; i++ ){
item = carList.get(i++);
System.out.print ("car Number: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Name: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Type: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Price : " + item + " ");
item = carList.get(i);
System.out.print ("& car model: " + item + " ");
System.out.println ();

}}
catch(IndexOutOfBoundsException e){
System.out.println(" ");
}

3
car_Display();
}

public void car_file(){

String filename = "filebin";


try{
String m ;
FileOutputStream fileos = new FileOutputStream(filename);
ObjectOutputStream os = new ObjectOutputStream(fileos);
for(int i=0 ; i<count-1 ;i++){
m = carList.get(i);
byte b[] = m.getBytes();
os.write(b);
String n = "\r\n";
os.writeBytes(n);

//os.write("\r\n".getBytes()); // makes a new line in the file


}

os.close();
System.out.println (" saved successfully!");

}
catch(FileNotFoundException e){
System.out.println (" ");
}
catch(IOException e){
System.out.println (" ");
}
catch(Exception e){
System.out.println (" ");
}
// car_Display();

}
public void car_show(){

String item ;
try{
for(int i=0 ; i<count-1; i++ ){
item = carList.get(i++);
System.out.print ("car Number: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Name: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Type: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Price : " + item + " ");
item = carList.get(i);
System.out.print ("& car model: " + item + " ");
System.out.println ();

4
}}
catch(IndexOutOfBoundsException e){
System.out.println(" ");
}
}

public void car_fread(){

String filename = "filebin";


try{
String m ;
FileInputStream fileis = new FileInputStream(filename);
ObjectInputStream is = new ObjectInputStream(fileis);
// m = is.readLine();
String line = is.readLine();
System.out.println (" Success saved!" + "\n" );

while (line != null){

System.out.print ("car Number: " + line + " ");


line = is.readLine();
System.out.print ("& car Name: " + line + " ");
line = is.readLine();
System.out.print ("& car Type: " + line + " ");
line = is.readLine();
System.out.print ("& car Price : " + line + " ");
line = is.readLine();
System.out.print ("& car model: " + line + " ");
System.out.println ();

line = is.readLine();
}

is.close();

}
catch(FileNotFoundException e){
System.out.println (" ");
}
catch(IOException e){
System.out.println (" ");
}
catch(Exception e){
System.out.println (" ");
}
//car_Display();

public void car_update(){


5
int x = 0 ;
String c_name;
Scanner input = new Scanner(System.in);

car_show();

System.out.println("Enter car namber :- ");


x = input.nextInt();

if(x != 1){
x--;
x = x*5;
x++;
}
try{
System.out.println("Enter car name :- ");
c_name = input.next();
carList.set(x++, c_name);
System.out.println("Enter car Type :- ");
c_name = input.next();
carList.set(x++, c_name);
System.out.println("Enter car Price :- ");
c_name = input.next();
carList.set(x++, c_name);
System.out.println("Enter car model :- ");
c_name = input.next();
carList.set(x, c_name);
}
catch(IndexOutOfBoundsException e){
System.out.println(" ");
}
System.out.println("update Done !");
car_Display();

public void car_delete(){

int x = 0 ;
Scanner input = new Scanner(System.in);
car_show();
System.out.println("Enter car namber :- ");
x = input.nextInt();

if(x != 1){
x--;
x = x*5;
}
try{
carList.remove(x);
carList.remove(x);
carList.remove(x);
carList.remove(x);
carList.remove(x);

6
System.out.println("delete Done !");
}
catch(IndexOutOfBoundsException e){
System.out.println(" ");
}
car_Display();

public void car_Search(String car_ser){

String item ;

//System.out.println ( carList.size()) ;
Boolean success = false;
try{
for(int i=0 ; i<count-1; i++ ){

if(car_ser.equals(carList.get(i))){
success = true;
i--;
System.out.println (car_ser + " is exists..!" );
item = carList.get(i++);
System.out.print ("car Number: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Name: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Type: " + item + " ");
item = carList.get(i++);
System.out.print ("& car Price : " + item + " ");
item = carList.get(i);
System.out.print ("& car model: " + item + " ");
System.out.println ();
break; }}

if(success == false){

System.out.println (car_ser + " dosen't exists..! " );


}}
catch(Exception e){
System.out.println ();
}

//car_Display();
}

public void car_Display(){

int menus;
Scanner input = new Scanner(System.in);
7
System.out.println("================ Welcome to car System
:==================");
System.out.println("Enter the number of operation ");
System.out.println("Enter 1 for Add a car ");
System.out.println("Enter 2 for Add a Toyota's car ");
System.out.println("Enter 3 for Update a car ");
System.out.println("Enter 4 for Search a car ");
System.out.println("Enter 5 for Delete a car ");
System.out.println("Enter 6 for Print cars ");
System.out.println("Enter 7 for Read from Binary file ");
System.out.println("Enter 8 for Exit & save in file ");

try {

menus = input.nextInt();
String csearch ;

switch (menus) {
case 1:
car_add();
break;
case 2:
Toyota_car tc = new Toyota_car();
tc.car_add();
break;
case 3 :
car_update();
break;
case 4:
System.out.println ("Enter value you want to search :");
csearch =input.next();
car_Search(csearch);
break;
case 5:
car_delete();
break;
case 6:
car_print();
break;
case 7:
car_fread();
break;
case 8:
car_file();
break;

}
}
catch(Exception e)
{
System.out.println(e.toString());
}

8
}

} ;

class Toyota_car extends car {

private int t_count = 0;


java.util.List<String> carchildList = new
java.util.ArrayList<String>();

Toyota_car(){
super();
}

public void car_add(){


Scanner input = new Scanner(System.in);

if (this.carList.isEmpty()){
int i = 1;
car_id =1;
String car_no = Integer.toString(i);
carList.add(car_no);}

else{

//String p_index = carList.get(count);


String car_no = Integer.toString(car_id);
carList.add(car_no);
//System.out.println(p_index + " is ");
// int pro_index = Integer.parseInt(p_index);
}

System.out.println("Enter TOYOTA's car name :- ");


car_name = input.nextLine();
carList.add(car_name);

carList.add("TOYOTA");
System.out.println("Enter " + car_name + " Price :- ");
car_price = input.nextInt();
String car_cost = Integer.toString(car_price);
carList.add( car_cost);
System.out.println("Enter " + car_name + " car model :- ");
car_model = input.nextInt();
String car_amount = Integer.toString(car_model);
carList.add(car_amount);
count += 5;
car_id++;
this.t_count++;
car_Display();

}
};

9
public class carpro {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
car pro = new car();
pro.car_Display();

// TODO code application logic here


}

}
Q1: (10 marks) In chapter 1, you have studied the 7 key concepts of object-
oriented design. You are requested to think about any three of them you are
currently facing (or you faced in the past) then do the following:

1. State the them in your own word.

2. Give code example where you use them.

‫ اشرح عن اي ثالثة منها‬،‫ مبادئ اساسية في تصميم الكائنات الموجهة‬7 ‫في الوحدة االولى درسنا‬
:‫ و طبق عليها التالي‬w‫واجهت خالل البرمجة في هذه المادة‬
.‫ اشرح اي ثالثة منها بلغتك الخاصة بحيث يعكس استيعابك‬-1
.‫اذكري امثلة كود على كيفية استخدامها‬-2

1- The notion of class


A Class is like an object constructor, or a "blueprint" for creating objects. Also the class is
user defined prototype in which we cane create an object of it . so our class is container of
some car attribute tas care name id , that help us to declar many type of car in very fast way .
class car
{
int car_id;
String car_name ;
String car_type ;
int car_price ;
int car_model;
int count = 1;

java.util.List<String> carList = new java.util.ArrayList<String>();


//
10
car(){
System.out.println("");
}

1-
2- The Central role of the object
Our class is "class car "In the next line we declare an object of the class car
public static void main(String[] args) {
car pro = new car();
this men that we declare copy of the class car that contain all the attributes and the
instance value of the class. Also we can also create an object of a class and access it in
another class.
3- Encapsulation
Also in our project we use Encapsulation the incpculation allow us to hides data from other
class inherited classes . we can access to this data type using an object of the same class it
product or data in the class .
class car
{
private int car_id;
private String car_name ;
private String car_type ;
private int car_price ;
private int car_model;
private int count = 1;

class Toyota_car extends car {

private int t_count = 0;


java.util.List<String> carchildList = new java.util.ArrayList<String>();

11

You might also like