0% found this document useful (0 votes)
6 views4 pages

Class Test

The document contains two Java programming tasks demonstrating key concepts of object-oriented programming and basic array manipulation. The first task involves creating a 'Person' class with encapsulated attributes and a 'Main' class to manage user input and display person details. The second task requires writing methods to calculate the sum of even numbers in an integer array and find the largest element in a double array, with user interaction for input.

Uploaded by

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

Class Test

The document contains two Java programming tasks demonstrating key concepts of object-oriented programming and basic array manipulation. The first task involves creating a 'Person' class with encapsulated attributes and a 'Main' class to manage user input and display person details. The second task requires writing methods to calculate the sum of even numbers in an integer array and find the largest element in a double array, with user interaction for input.

Uploaded by

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

hGroup 9: FT In Class Test

Question
One of the key concepts of object-oriented programming (OOP) is
encapsulation, emphasizes concealing sensitive data from unauthorized access
or alteration, while setters and getters are provided for modification purposes.
To demonstrate this concept, create a Java program comprising two classes:
Person and Main. In the Person class, define the attributes of an individual,
including their first name, last name, age, gender, marital status, native
language, and specify a method that prints the person's particulars. In the Main
class, you should have at least two (2) objects and then utilize setters and getters
to assign and retrieve the values for these attributes, with the user providing the
input values for the first object. Then provide your own values for the second
object and invoke the print function on it.

Code
Person Class
public class Person {
private String firstName;
private String lastName;
private int age;
private String gender;
private String maritalStatus;
private String nativeLanguage;

public Person() {

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

public void setAge(int age) {


this.age = age;
}

public void setGender(String gender) {


this.gender = gender;
}

public void setMaritalStatus(String maritalStatus) {


this.maritalStatus = maritalStatus;
}
public void setNativeLanguage(String nativeLanguage) {
this.nativeLanguage = nativeLanguage;
}

public String getFirstName() {


return this.firstName;
}

public String getLastName() {


return this.lastName;
}

public int getAge() {


return this.age;
}

public String getGender() {


return this.gender;
}

public String getMaritalStatus() {


return this.maritalStatus;
}

public String getNativeLanguage() {


return this.nativeLanguage;
}

public void printPerson() {


System.out.println("First Name: " + this.firstName);
System.out.println("Last Name: " + this.lastName);
System.out.println("Age: " + this.age);
System.out.println("Gender: " + this.gender);
System.out.println("Marital Status: " + this.maritalStatus);
System.out.println("Native Language: " + this.nativeLanguage);
}
}

Main Class
import java.util.Scanner;

public class Main {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
Person person1 = new Person();
System.out.println("Enter First Name: ");
String firstName = input.next();
person1.setFirstName(firstName);

System.out.println("Enter Last Name: ");


String lastName = input.next();
person1.setLastName(lastName);

System.out.println("Enter Age: ");


int age = input.nextInt();
person1.setAge(age);

System.out.println("Enter Gender: ");


String gender = input.next();
person1.setGender(gender);

System.out.println("Enter Marital Status: ");


String maritalStatus = input.next();
person1.setMaritalStatus(maritalStatus);

System.out.println("Enter Native Language: ");


String nativeLanguage = input.next();
person1.setNativeLanguage(nativeLanguage);

Person person2 = new Person();


person2.setFirstName("John");
person2.setLastName("Doe");
person2.setAge(25);
person2.setGender("Male");
person2.setMaritalStatus("Single");
person2.setNativeLanguage("English");

System.out.println("Person 1: ");
person1.printPerson();

System.out.println("Person 2: ");
person2.printPerson();
}

Group 2: FT In Class Test


Question
Write a java program that has two methods. The first method is a static one that
accepts an integer array, calculates the sum of all even numbers within the array,
and the return the computed value. The second method is non-static, and it takes
a double array, which returns the largest element. The program should obtain
values from the user. Finally, print the calculated total, and the largest value.

Code
import java.util.Scanner;

public class Group2 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Obtaining integer array from user


System.out.print("Enter the size of the integer array: ");
int intSize = scanner.nextInt();
int[] intArray = new int[intSize];
System.out.println("Enter the elements of the integer array: ");
for (int i = 0; i < intSize; i++) {
intArray[i] = scanner.nextInt();
}
// Calculating sum of even numbers in integer array
int evenSum = sumEvenNumbers(intArray);
System.out.println("Sum of even numbers in the integer array: " +
evenSum);

// Obtaining double array from user


System.out.print("Enter the size of the double array: ");
int doubleSize = scanner.nextInt();
double[] doubleArray = new double[doubleSize];
System.out.println("Enter the elements of the double array: ");
for (int i = 0; i < doubleSize; i++) {
doubleArray[i] = scanner.nextDouble();
}

// Finding largest element in double array


double largestElement = findLargestElement(doubleArray);
System.out.println("Largest element in the double array: " +
largestElement);

scanner.close();
}

public static int sumEvenNumbers(int[] intArray) {


int evenSum = 0;
for (int i = 0; i < intArray.length; i++) {
if (intArray[i] % 2 == 0) {
evenSum += intArray[i];
}
}
return evenSum;
}

public static double findLargestElement(double[] doubleArray) {


double largestElement = doubleArray[0];
for (int i = 1; i < doubleArray.length; i++) {
if (doubleArray[i] > largestElement) {
largestElement = doubleArray[i];
}
}
return largestElement;
}

You might also like