0% found this document useful (0 votes)
2 views3 pages

Sheet2 Code

The document contains Java code for eight problems, each implemented in separate classes within the 'sheet2' package. The problems include calculating discounts, exponentiation, multiplication tables, grade calculations, distance between points, average of an array, product of even and odd numbers, and finding the maximum value in an array. Each problem has a main method demonstrating its functionality with sample inputs.

Uploaded by

5g5vwgnqd9
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)
2 views3 pages

Sheet2 Code

The document contains Java code for eight problems, each implemented in separate classes within the 'sheet2' package. The problems include calculating discounts, exponentiation, multiplication tables, grade calculations, distance between points, average of an array, product of even and odd numbers, and finding the maximum value in an array. Each problem has a main method demonstrating its functionality with sample inputs.

Uploaded by

5g5vwgnqd9
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

package sheet2;

public class Problem1 {


public static double disFun(double purchaseAmount) {
double discount;
if (purchaseAmount > 1000) {
discount = 0.15;
} else {
discount = 0.05;
}
return purchaseAmount * (1 - discount);
}

public static void main(String[] args) {


double fixedAmount = 1200.0;
double finalPrice = disFun(fixedAmount);
[Link]("Purchase after discount: " + finalPrice);
}
}

package sheet2;

public class Problem2 {


public static double powFun(double num1, double num2) {
return [Link](num1, num2);
}

public static void main(String[] args) {


double num1 = 5.0;
double num2 = 3.0;
double result = powFun(num1, num2);
[Link](num1 + "^" + num2 + " = " + result);
}
}

package sheet2;

public class Problem3 {


public static void multTable(int number) {
for (int i = 1; i <= 10; i++) {
[Link](number + " x " + i + " = " + (number * i));
}
}

public static void main(String[] args) {


int fixedNumber = 7;
multTable(fixedNumber);
}
}

package sheet2;

public class Problem4 {


public static void gradeFun(double programming, double discrete,
double math, double linear, double physics) {
double total = programming + discrete + math + linear + physics;
double percentage = total / 5;
[Link]("Percentage: " + percentage + "%");

if (percentage >= 90) {


[Link]("Grade: A");
} else if (percentage >= 80) {
[Link]("Grade: B");
} else if (percentage >= 70) {
[Link]("Grade: C");
} else if (percentage >= 60) {
[Link]("Grade: D");
} else if (percentage >= 50) {
[Link]("Grade: E");
} else {
[Link]("Grade: F");
}
}
}

package sheet2;

public class Problem5 {


public static float distance(float x1, float y1, float x2, float y2) {
return (float) [Link]([Link](x2 - x1, 2) + [Link](y2 - y1, 2));
}

public static void main(String[] args) {


float x1 = 3.0f, y1 = 4.0f;
float x2 = 7.0f, y2 = 1.0f;

float dist = distance(x1, y1, x2, y2);


[Link]("Distance between points: " + dist);
}
}

package sheet2;

public class Problem6 {


public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int sum = 0;

for (int num : numbers) {


sum += num;
}

double average = (double) sum / [Link];


[Link]("Average: " + average);
}
}

package sheet2;

public class Problem7 {


public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int evenProduct = 1;
int oddProduct = 1;
for (int num : numbers) {
if (num % 2 == 0) {
evenProduct *= num;
} else {
oddProduct *= num;
}
}

[Link]("Product of even numbers: " + evenProduct);


[Link]("Product of odd numbers: " + oddProduct);
}
}

package sheet2;

public class Problem8 {


public static int maxFun(int[] arr) {
int max = arr[0];
for (int i = 1; i < [Link]; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

public static void main(String[] args) {


int[] numbers = {23, 45, 12, 67, 34};
int maxValue = maxFun(numbers);
[Link]("Maximum value: " + maxValue);
}
}

You might also like