0% found this document useful (0 votes)
18 views

CMP 322 Java Assignment

Uploaded by

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

CMP 322 Java Assignment

Uploaded by

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

Name: Onah Lawrence

Matric No: 17/40945/UE


Course Option: Statistics/Computer Science Education
Course Code: CMP322 (Object Oriented Programing)
Question 1: Write a program to calculate the mean value from 2.8, 6, 7.3, 4.7, 9.5 and P if
mean−standard deviation
p=
mean

Java Code;
public class Main {
public static void main(String[] args) {
double[] numbers = {2.8, 6, 7.3, 4.7, 9.5};
double mean = calculateMean(numbers);
double stdDev = calculateStandardDeviation(numbers, mean);
double P = calculateP(mean, stdDev);
System.out.println("Mean: " + mean);
System.out.println("Standard Deviation: " + stdDev);
System.out.println("P: " + P);
}

public static double calculateMean(double[] numbers) {


double sum = 0;
for (double num : numbers) {
sum += num;
}
return sum / numbers.length;
}

public static double calculateStandardDeviation(double[] numbers, double mean) {


double sum = 0;
for (double num : numbers) {
sum += Math.pow(num - mean, 2);
}
return Math.sqrt(sum / numbers.length);
}

public static double calculateP(double mean, double stdDev) {


return mean - stdDev / mean;
}
}
OUTPUT SCREEN
Question 2: Write a program to calculate the area of 6 different trapeziums and obtain their mean
T
mean deviation+30000
deviation and R = R=( ) if T=0,1,2,3,4,5
mean
R*= standard deviation of R

Java Code;
public class Main {
public static void main(String[] args) {
// Define the dimensions of the 6 trapeziums
double[][] trapeziums = {
{3, 4, 5}, // a, b, h
{6, 8, 10},
{1, 3, 2},
{9, 12, 15},
{4, 6, 8},
{2, 5, 7}
};

// Calculate the area of each trapezium


double[] areas = new double[trapeziums.length];
for (int i = 0; i < trapeziums.length; i++) {
areas[i] = calculateArea(trapeziums[i][0], trapeziums[i][1], trapeziums[i][2]);
System.out.println("Trapezium " + (i+1) + ": Area = " + areas[i]);
}

// Calculate the mean of the areas


double mean = calculateMean(areas);
System.out.println("Mean: " + mean);

// Calculate the mean deviation


double meanDeviation = calculateMeanDeviation(areas, mean);
System.out.println("Mean Deviation: " + meanDeviation);

// Calculate R for T = 0, 1, 2, 3, 4, 5
for (int T = 0; T <= 5; T++) {
double R = calculateR(meanDeviation, mean, T);
System.out.println("T = " + T + ": R = " + R);
}
}

public static double calculateArea(double a, double b, double h) {


return (a + b) / 2 * h;
}

public static double calculateMean(double[] values) {


double sum = 0;
for (double value : values) {
sum += value;
}
return sum / values.length;
}

public static double calculateMeanDeviation(double[] values, double mean) {


double sum = 0;
for (double value : values) {
sum += Math.abs(value - mean);
}
return sum / values.length;
}

public static double calculateR(double meanDeviation, double mean, double T) {


return Math.pow(meanDeviation + 30000 / mean, T);
}
}

OUTPUT SCREEN

You might also like