Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views2 pages

Java Scenario Answers Easy

The document contains five Java code examples demonstrating basic programming concepts. These include calculating the cost per mango, printing book titles, averaging temperatures, checking for leap years, and calculating the total cost in a shopping cart. Each example is self-contained and illustrates fundamental Java syntax and operations.

Uploaded by

apsammu23
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 views2 pages

Java Scenario Answers Easy

The document contains five Java code examples demonstrating basic programming concepts. These include calculating the cost per mango, printing book titles, averaging temperatures, checking for leap years, and calculating the total cost in a shopping cart. Each example is self-contained and illustrates fundamental Java syntax and operations.

Uploaded by

apsammu23
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/ 2

Scenario-Based Java Answers (Easy

Level)
1.
```java
public class MangoCostCalculator {
public static void main(String[] args) {
int totalCost = 120;
int mangoes = 12;
int costPerMango = totalCost / mangoes;
System.out.println("Cost per mango: ₹" + costPerMango);
}
}
```

2.
```java
public class BookTitles {
public static void main(String[] args) {
String[] books = {"Java Basics", "OOP Concepts", "Data Structures"};
for (String book : books) {
System.out.println(book);
}
}
}
```

3.
```java
public class TemperatureAverage {
public static void main(String[] args) {
int[] temps = {30, 32, 31, 29, 28, 33, 30};
int sum = 0;
for (int temp : temps) {
sum += temp;
}
double avg = sum / (double) temps.length;
System.out.println("Average temperature: " + avg);
}
}
```

4.
```java
public class LeapYearChecker {
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static void main(String[] args) {
int year = 2024;
System.out.println(year + " is leap year? " + isLeapYear(year));
}
}
```

5.
```java
import java.util.ArrayList;

public class ShoppingCart {


public static void main(String[] args) {
ArrayList<Double> prices = new ArrayList<>();
prices.add(99.99);
prices.add(49.50);
prices.add(29.99);

double total = 0;
for (double price : prices) {
total += price;
}
System.out.println("Total cost: ₹" + total);
}
}
```

You might also like