Professional Documents
Culture Documents
OOP LAb 7
OOP LAb 7
OOP LAb 7
Adnan Ahmad
Registeration No. :
FA1B-BCE-030
Section:
B
Object Oriented Programming Lab
Lab 07
Arrays
Task 1:
Make an array of n elements and initialize it, data and size of the array shall be given by user. Print all
elements, print minimum number and its index in array, print maximum number and its index in the
array and also print the average number.
Code:
package arrays;
import java.util.Scanner;
int n;
int index_of_smallest_number=0;
int index_of_largest_number=0;
n=input.nextInt();
array[i]=input.nextInt();
System.out.println(" ");
System.out.println(array[i]);
System.out.println(" ");
if(array[i] <min)
min = array[i];
index_of_smallest_number=i;
System.out.println(" ");
index_of_largest_number=i;
Output:
Task 2
Make a class Course with following attributes courseCode, courseTitle and courseCreditHr. All attributes
are private and exposed with getter and setter. In main class, create an array of courses. Ask user to
input number of courses and then for each course, get all data from user. Once user input is done, print
information of courses in following format:
Course 1 Data:
Code:
Main Class:
package courses;
import java.util.Scanner;
int n=input.nextInt();
System.out.println(" ");
String CourseCode=inputc.nextLine();
String CourseTitle=inputc.nextLine();
int CourseCreditHrs=input.nextInt();
System.out.println(" ");
}}
Course Class:
package courses;
this.courseCode = courseCode;
return courseCode; }
return courseTitle;
this.courseTitle = courseTitle;
return courseTitle;
return courseCreditHrs;
this.courseCreditHrs = courseCreditHrs;
return courseCreditHrs;}
Output:
Task 4:
Make a class Course with attributes courseTitle and 2-dimensional int array of marks.
i.e.
First user will input total number of students and then number of test for each student. Create a method
inputMarksData() which will input marks of all student in a course. Create another method
displayStudentData() which will print courseTitle and marks of each student. In main class instantiate
object of Course class, ask user to inpur courseTitle and call both methods.
Code:
Main Class:
package courseclass;
import java.util.Scanner;
course.courseTitle = sc.nextLine();
course.InputMarksData();
course.DisplayStudentData();
Course Class:
package courseclass;
import java.util.Scanner;
public Course() {
marks[i][j] = sc.nextInt();
void DisplayStudentData() {
System.out.println();
System.out.println();
Output: