Wednesday, December 4, 2019

Java Programming Principles

Questions: Task 11.1 Discuss the principles, characteristics and features of programming in Java.1.2 Critically evaluate the environmental flexibility of programming in Java.Task 22.1 Design a Java programming solution based on the above scenario.2.2 Explain the components, data and file structures required to implement a given design.Task 33.1 Implement a Java programming solution based on a prepared design.3.2 Define relationships between objects to implement design requirements3.3 Implement object behaviours using control structures to meet the design algorithms.3.4 Identify and implement opportunities for error handling and reporting.3.5 Make effective use of an integrated development environment including code and screen templatesTask 44.1 Critically review and test a Java programming solution.4.2 Analyse test results against expected results to identify discrepancies.4.3 Evaluate independent feedback on a developed Java program solution and make recommendations for improvements.4.4 Create user documentation for the development of Java programming solution.4.5 Create technical documentation for the support and maintenance of a Java programming solution Answers: Introduction Java is an example of an OOP (Object Oriented Program) (Klein, 2005). So it must support all the basic principles of an OOP language (Okal, 2012). Thus the principles of java and hence of an OOP language is- Encapsulation: This means binding together of date and then putting them under a single block. In a car, there are a large number of systems that co-exist together(Mittal, 2013). Changing the gears does not turn on the headlights and bulling brakes does not accelerate the car. Hence all these systems are at the same place and are controlled by a single person (driver) but none is interfering with each other with their functioning. The basic example of encapsulation can be obtained from the classes that are used in java. These classes contain the entire block of code within them and no other program can access them unless specific rules are followed. If the methods of a class are declared private, them they cannot be overwritten(Lakkis, 2007). Also if a class is declared private, then the class can only be inherited and no object creation can be allowed for that class anywhere within the program. Hence that class and its methods are protected by encapsulation (Rebek, 2005). Inheritance: This is another OOP principle which enables the program to inherit the properties from another class (Flanagan, 2005). The inherited class is called the parent class while the classes that inherit the parent class are termed as child class. Now for inheritance the child class gets all the methods and variables of the parent class. Also the Object of the parent class can be created with the reference of the child (Raposa, 2003). There are two forms of inheritance-multiple, multi-level. In multiple inheritances, more than one class can be inherited at the same time. Consider an example in case of an airport an aircraft uses its landing gear to make turns, take-off, and land on the landing strip. Also it is primarily used for flying. In that respect the aircraft is a flying vehicle and it is also a ground-vehicle (since it can move on the landing strips) (Link and Frlich, 2003). Hence the aircraft inherits the properties of both these classes at the same time. This is multiple inheritan ces. In multi-level inheritances, only one class can be inherited a t a time. But there is a type of hierarchy.This means that if A class inherits B, then C inherits A, C gets the methods of A by default. Example golden retriever is a dog, which in turn is a mammal which in turn is an animal. So this makes Golden terrier a mammal, an animal, and also a dog (Cadenhead, 2012). This is termed as multi-level inheritance. Polymorphism: This word comes from the Greek words poly meaning means many, morph means form. Hence polymorphism means many forms (Eckel, 2000). This means that a single problem in java can be approached from three different ends. In a normal programming language, that does not implement any OOP principles (Link and Frlich, 2003), if we were to implement a stack for different data types, then we would have needed 3 or four different structures with different names. In each case the same algorithm is being used only the data types used are different. But in JAVA we can name all these under the same implementation of stack (Sierra and Bates, 2005). Hence our work is greatly reduced and we are also able to use the same name for different implementations of the same program. In JAVA polymorphism is simply implanted by the use of interfaces (Byrne and Cross, 2009). If a class implements an interface then it must implement the methods that are written in the interface. Characteristics of java: It is simple to code and decode. It is an object oriented programming language (Hubbard, 2001). It is highly secure. It can be implemented on any form of machine. The main reason for this is java compiles the program into byte code (Riccardi, 2001). Now this byte code file is not compiler dependent, hence the file can run on Windows, Linux, and Mac etc. It has in-built support for multithreading. The most important part of java program comes from its usage to code applets and hence is used extensively for coding websites. It can be used to code and run graphical applications. It is robust as it is error-free and has severe typing restrictions and also various run-time checking methods are used (Wampler, 2002). It supports the use of interfaces. It can be easily linked to any other program. There is inbuilt support for garbage collection and it has a very simplified exception handling capabilities. Task 1.2 JAVA provides a great flexibility to the programmers. Often when we create program, other people may change the basic functions created, or overwrite them. This may lead to crashing of the program (Horstmann, 2008). In JAVA, this problem is completely avoided by adding the prefix final; as a result this class or method to which the prefix is added cannot be altered in any way. Also the use of interfaces makes programming easier (Horstmann, 2005). Any class in java that implements the interface must overwrite the methods that are mentioned within the interface. Also the use of keyword abstract, allows the user to use declared methods without any sort of body. Also any class that inherits this abstract class must override this method within it. The garbage correction algorithm of java is also very unique and advanced. There is no need for releasing the allocated memories (as in case of C++), because in java, when the compiler cannot find the reference of any object it deletes the objec t any kind of variables that it was holding (Morelli, 2000). Also the exception handling capabilities of java is extremely helpful. In C, when there was an arithmetic exception like division by 0, the program used to hang and had to be stopped manually, but in JAVA, the program gives the location of the place and also the cause of the problem and exits the program. This is extremely helpful for debugging large programs. Task 2.1 The above task has told us to design a java program that can act as a student database manager. It must be able to take in the students first name, last name, their date of birth, their ID number, course enrolled by them, mobile number, their address etc (Lewis and Chase, 2005). Now the administrator must be able to view the details of any student he wishes. He must also be able to delete the entire database, or a single student, update and also enter the details of any student. In order to do all this with a JAVA program, we must first build a class named student that can contain these variables with data within it. This class can then be manipulated from the main class by creating an object for each student. Then all these objects must be linked somehow to form an entire list of students which are really classes with each one holding their own respective data. Also care must be taken to note that the entire system can only handle a maximum number of students. Task 2.2 In the given problem, we are describing a class named Student that has the variables for holding data like last name, first name, DOB, Address, mobile number, etc. All these variables have their respective data types like address is stored in a string, DOB in float, etc. Now, in the main program named, StudentDatabaseManagement an array of Student objects are created of size 200. This means that the database is capable of holding 200 students. This number can be increased or decreased by editing the array size of the object array in the main program. Now, we use a switch case algorithm which allows us to have many options like entering details of a student, clearing the database, deleting a student, viewing the students etc. In the main program, we have a special variable that is used to track the number the number of students. This number is incremented when a new student is admitted to the database. Now also in each student object there is a variable to track the number at which he / she has been admitted into the database. This is important because in case of deletion, a space in the middle of the array is empty (Deitel and Deitel, 2012). Hence, in such a case we must shift the students below that space to make the array a continuous one with no empty spaces in between. As a result the array can only be empty from the end. There should not be any gap in between the array to conserve the space. The algorithm required to shift the students one space above is located under the case that is responsible for deleting the students and it also decrements the variable that is used to track the total number of students in the array. Also, proper control checks are provided at each level so that the age limit for entry into the institution can be set. This is done by using a variable that holds the year after which the candidate must be born in order to take admission. If the year is before that specified a error message is displayed. Various exception checks are also p rovided so that proper data is entered into the proper cases. When the entire database is empty, then a proper error message is displayed and similar thing when the database is empty. Task 3.1 The code of the main program is given below. It consists of two classes namely Student, StudenyDatabaseManagement (main class). StudenyDatabaseManagement packagestudentdatabasemanagement; importjava.util.Scanner; public class StudentDatabaseManagement extends Exception{ public static void main(String[] args) { int index=0; int cases; boolean switching=true; intallowed_date=1988;//sets age limit for entry Student[] studentdatabase=new Student[200]; Student[] studentdatabasecopy=new Student[200]; for(int i=0;i10;i++) { studentdatabase[i]=new Student(); studentdatabasecopy[i]=new Student(); } Scanner s=new Scanner(System.in); while(switching){ System.out.println("Enter your choice:n1)Enter Studentn2)See all Studentsn3)see single Student detail"); System.out.println("4)Delete all studentsn5)Updaten6)Delete single studentn7)Exit"); System.out.println("Enter your choice"); cases=s.nextInt(); switch(cases){ case 1: { try { if(index==10){ throw new Exception(); } } catch(Exception a){ System.out.println("Database full"); } index++; studentdatabase[index]=new Student(); System.out.println("Enter the roll number of the student(max 10 digits):"); studentdatabase[index].stud_roll=s.nextInt(); s.nextLine(); System.out.print("Enter the last name of the student:"); studentdatabase[index].stud_lastname=s.nextLine(); System.out.print("nEnter the course under which student is enrolled:"); studentdatabase[index].stud_course=s.nextLine(); System.out.println("Enter the month when student was born in digits:"); studentdatabase[index].stud_mon=s.nextInt(); while(studentdatabase[index].stud_mon12) { System.out.println("Wrong Data, Re-Enter:"); studentdatabase[index].stud_mon=s.nextInt(); } System.out.println("Enter the date when student was born in digits:"); studentdatabase[index].stud_date=s.nextInt(); while(studentdatabase[index].stud_date31||(studentdatabase[index].stud_date28studentdatabase[index].stud_mon==2)) { System.out.println("Wrong Data, Re-Enter:"); studentdatabase[index].stud_date=s.nextInt(); } System.out.println("Enter the year when student was born in digits:"); studentdatabase[index].stud_year=s.nextInt(); if(studentdatabase[index].stud_yearallowed_date) { System.out.println("Your ward is not admissable"); switching=false; break; } s.nextLine(); System.out.print("Enter the first name of the student:"); studentdatabase[index].stud_firstname=s.nextLine(); System.out.println("nEnter the mobile number of the student:"); studentdatabase[index].stud_mob_no=s.nextLong(); s.nextLine(); System.out.print("Enter the address of the student:"); studentdatabase[index].stud_add=s.nextLine(); studentdatabase[index].stud_no=index; break; } case 2: { if(index==0){ System.out.println("Database Empty"); } else { for(int count=1;count=index;count++){ System.out.println("Name of student: "+studentdatabase[count].stud_firstname+" "+studentdatabase[index].stud_lastname); System.out.println("Roll no: "+studentdatabase[count].stud_roll); System.out.println("Course enrolled: "+studentdatabase[count].stud_course); System.out.println("DOB: "+studentdatabase[count].stud_date+"/"+studentdatabase[count].stud_mon+"/"+studentdatabase[count].stud_mon); System.out.println("Address: "+studentdatabase[count].stud_add); System.out.println("Mob no: "+studentdatabase[count].stud_mob_no); } } break; } case 3: { try{ if(index==0){ throw new Exception(); } } catch(Exception a){ System.out.println("Database is empty."); break; } int roll2; System.out.println("Enter student roll no:"); roll2=s.nextInt(); for(int count=1;count=index;count++){ if(studentdatabase[count].stud_roll==roll2){ System.out.println("Name of student:"+studentdatabase[count].stud_firstname+" "+studentdatabase[index].stud_lastname); System.out.println("Roll no:"+studentdatabase[count].stud_roll); System.out.println("Course enrolled:"+studentdatabase[count].stud_course); System.out.println("DOB: "+studentdatabase[count].stud_date+"/"+studentdatabase[count].stud_mon+"/"+studentdatabase[count].stud_mon); System.out.println("Address"+studentdatabase[count].stud_add); System.out.println("Mob no:"+studentdatabase[count].stud_mob_no); break; } } break; } case 4: index=0; break; case 5: { System.out.println("Enter the roll number to update the records"); int roll=s.nextInt(); for(int i=1;i=index;i++) { if(roll==studentdatabase[i].stud_roll) { System.out.println("Enter the roll number of the student(max 10 digits):"); studentdatabase[index].stud_roll=s.nextInt(); s.nextLine(); System.out.print("Enter the last name of the student:"); studentdatabase[index].stud_lastname=s.nextLine(); System.out.print("nEnter the course under which student is enrolled:"); studentdatabase[index].stud_course=s.nextLine(); System.out.println("Enter the month when student was born in digits:"); studentdatabase[index].stud_mon=s.nextInt(); while(studentdatabase[index].stud_mon12) { System.out.println("Wrong Data, Re-Enter:"); studentdatabase[index].stud_mon=s.nextInt(); } System.out.println("Enter the date when student was born in digits:"); studentdatabase[index].stud_date=s.nextInt(); while(studentdatabase[index].stud_date31||(studentdatabase[index].stud_date28studentdatabase[index].stud_mon==2)) { System.out.println("Wrong Data, Re-Enter:"); studentdatabase[index].stud_date=s.nextInt(); } System.out.println("Enter the year when student was born in digits:"); studentdatabase[index].stud_year=s.nextInt(); if(studentdatabase[index].stud_yearallowed_date) { System.out.println("Your ward is not admissable"); switching=false; break; } s.nextLine(); System.out.print("Enter the first name of the student:"); studentdatabase[index].stud_firstname=s.nextLine(); System.out.println("nEnter the mobile number of the student:"); studentdatabase[index].stud_mob_no=s.nextLong(); s.nextLine(); System.out.print("Enter the address of the student:"); studentdatabase[index].stud_add=s.nextLine(); } } break; } case 6: { System.out.println(" Enter the roll of the student to be deleted:"); introlldel=s.nextInt(); for(int i=0;iindex;i++) { if(studentdatabase[i].stud_roll!=rolldel) { studentdatabasecopy[i]=studentdatabase[i]; } else { for(int j=i;j(index-1);j++) { studentdatabasecopy[j]=studentdatabase[j+1]; } for(int k=0;kindex;k++) { studentdatabase[i]=studentdatabasecopy[i]; }index--; break; } } break; } case 7: switching=false; break; } } } } Student package studentdatabasemanagement; public class Student { String stud_firstname; String stud_lastname; longstud_mob_no; int stud_roll; intstud_no; String stud_add; String stud_course; intstud_mon; intstud_date; intstud_year; } Task 3.2 In this program we are defining a special class that can hold the different data like DOB, first name, last name, address, etc. Now all these have different variables with respective data types. In the main program we are creating an array of 200 students (Shankar, 2013). Hence the net size of the database created is 200. But defining an array is not enough, in the switch case menu the case that is responsible for the entry of new students, for each student a new "student" class object is defined using the "new" keyword. All the objects created have a reference under the array of "student" classes. Hence each time we want to view a student, we are actually calling to see the data held by each of these objects. The students are tracked by a special variable called index. The value of this index variable gives us the number of objects in the database (Horstmann, 2012). Now this index is each time edited when we enter a new students details. At this point we increment index (Horstmann, 2013). Also each of the students has a number allotted to it called student_no. This is kind of a serial number that gets replaced during deletion. Task 3.3 Various control structures were used in the above program. These included looping systems like while, for loops and also other control statements like if, switch etc. The switch case, which was enclosed in an infinite while loop, was used to switch between the various tasks that were to be performed by the program. The infinite while loop ensured that the switch case statements were shown continuously. Also if the infinite while was not used then to view, enter or update the details of a student each time the program had to be restarted. This can be quite tedious, to say the least (Barnes, 2000). This infinite while loop was maintained by enclosing a Boolean variable (whose value was set to be true) within the condition of the loop. There was an exit condition among the cases that turned this Boolean value to false, hence when the loop tried to run the next time, it exited because while (false) is not allowed. In various cases, the if control statement was used, for checking various values, like if the date of birth was entered 31(by mistake), or the month was entered12 (by mistake). If they were not then (that means correct date and month were applied) then the control statement was avoided. Else the control was transferred to the statements written within the control loop (Keith and Schincariol, 2006). Task 3.4 Exception handling mechanisms are quite useful in java programs. There are many kinds of exceptions in java namely Arithmetic Exception, Array out of bounds exception, etc. They are used to detect the unwanted cases that may cause an error in the program, which in turn may result in crashing of the program. The exception handling capabilities of java are very unique. In case of other programming languages (like c), the program would have crashed if a fatal error like divide by 0 would have occurred. But in Java this does not occur. When java finds an exception in a program, then it stops the execution at that point and throws the exception to the exception handling mechanism. The exception handling mechanism copies the line number where it finds an exception and sends it to the user on the screen along with the cause of the error. Also in case of java we can define our own exceptions and hence not depend on the program to give its own values. Example the age of a man cannot be negati ve, hence in a program if someone entered a negative age then by setting up a proper exception, we can define a proper exception and hence the user gets an error when he/she is trying to enter the incorrect age.In this program, we have used exception handling capabilities of java to make our own exceptions. Here we have used the concept of exception handling at two places. These are checking if the array is full or if it is empty (Reiersl, Baker and Shiflett, 2007). This is quite important as we must make sure that the array is not full each time we try to enter a new student. This is important as if it was not checked, and then the compiler would throw us an exception for overstepping the array bounds. Task 3.5 In this programming an IDE was used. An IDE or Integrated Development Environment is a platform that is used extensively by programmers and software developers alike for development of particular software or even a program.Netbeans was the IDE that was used in the development of this project. The use of IDE has made programming very easy and comfortable. In normal case we could have coded the program in a notepad and then run the program on command prompt to see if there were any errors. But this can be difficult as the command prompt does not give us specific results to what the problem is. It merely gives us the error that was given by the exception handling part of Java runtime. Now for a beginner this can be quite difficult as he is not fully acquainted with all the programming terms. Also the command prompt window does not give any hint to what might have caused the error. But his use of an IDE eliminated all these difficulties. In an IDE, the lines that could an error are detec ted straightaway when they are written by the programmer. In case of using a command prompt, the programmer would have to wait until running the program to get the errors. Also the IDE reduce coding efforts by providing various suggestions according to which a thing can be coded. For example in Netbeans, if we write so and then press Ctrl+Spacebar on the keyboard. Then the written word transforms into System.out.println(). Also during writing variables for loops the available and valid ones were shown on pressing the hint button. Below a few screenshots of the program on Netbeans is shown.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.