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

Java Coding Tasks and Solutions

The document contains three Java programming tasks: the first task implements a piecewise function based on the value of x, the second calculates the average of even numbers less than 20, and the third uses a switch statement to print corresponding messages based on student grades from A to E. Sample code is provided for each task to demonstrate the implementation. The code includes user input handling and basic control structures.

Uploaded by

laila.alheilat
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)
5 views3 pages

Java Coding Tasks and Solutions

The document contains three Java programming tasks: the first task implements a piecewise function based on the value of x, the second calculates the average of even numbers less than 20, and the third uses a switch statement to print corresponding messages based on student grades from A to E. Sample code is provided for each task to demonstrate the implementation. The code includes user input handling and basic control structures.

Uploaded by

laila.alheilat
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

Java Lab Task

1. Write code to implement the following equation:

Fx =
x + 7 , if x < 10
x^2 + 3 , if x == 10
x^3 + 5 , if x > 10

Sample Java Code:

import [Link];

public class Task1 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter value of x: ");
int x = [Link]();
int Fx;

if (x < 10) {
Fx = x + 7;
} else if (x == 10) {
Fx = x * x + 3;
} else {
Fx = x * x * x + 5;
}

[Link]("Fx = " + Fx);


}
}

2. Write Java code to calculate the average of even numbers less than 20.

Sample Java Code:

public class Task2 {


public static void main(String[] args) {
int sum = 0, count = 0;
for (int i = 1; i < 20; i++) {
if (i % 2 == 0) {
sum += i;
count++;
}
}
double average = (double) sum / count;
[Link]("Average of even numbers less than 20 = " + average);
}
}

3. Write Java code to enter the student grade, then print the following sentence using switch
statement.

A -> Excellent
B -> Very Good
C -> Good
D -> Passed
E -> Failed

Sample Java Code:

import [Link];

public class Task3 {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter grade (A-E): ");
char grade = [Link]().charAt(0);

switch (grade) {
case 'A':
[Link]("Excellent");
break;
case 'B':
[Link]("Very Good");
break;
case 'C':
[Link]("Good");
break;
case 'D':
[Link]("Passed");
break;
case 'E':
[Link]("Failed");
break;
default:
[Link]("Invalid grade");
}
}
}

You might also like