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

Informatics 1a

Informatics

Uploaded by

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

Informatics 1a

Informatics

Uploaded by

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

Question 1

import javax.swing.JOptionPane;

public class BankBalanceDoWhile {


public static void main(String[] args) {
double investmentAmount =
Double.parseDouble(JOptionPane.showInputDialog("Enter the investment amount:"));

int year = 1;
int choice;

do {
choice = Integer.parseInt(JOptionPane.showInputDialog("Do you wish to
see the balance after year " + year + "?\nEnter 1 for Yes, 0 to exit:"));

if (choice == 1) {
double balance = calculateBalance(investmentAmount, year);
JOptionPane.showMessageDialog(null, "Balance after year " + year +
": $" + balance);
year++;
} else if (choice != 0) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter 1
to view balance or 0 to exit.");
}
} while (choice != 0);
}

public static double calculateBalance(double investmentAmount, int year) {


double interestRate = 0.05; // Assuming 5% interest rate
double balance = investmentAmount;

for (int i = 1; i <= year; i++) {


balance += balance * interestRate;
}

return balance;
}
}

Question 2

import javax.swing.JOptionPane;

public class NumberLineTriangle {


public static void main(String[] args) {
int base = Integer.parseInt(JOptionPane.showInputDialog("Enter the
height/base of the triangle:"));

System.out.println("You have entered the height/base of " + base);

for (int i = 0; i <= base; i++) {


for (int j = 0; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}

Question 3

import javax.swing.JOptionPane;

public class Decision {


public static void main(String[] args) {
int count = 1;

while (count <= 5) {


int mark = Integer.parseInt(JOptionPane.showInputDialog("Enter mark " +
count + ":"));

if (mark >= 50 && mark <= 100) {


System.out.println(mark + " is a pass");
} else {
System.out.println(mark + " is a fail");
}

count++;
}
}
}

Question 4

import javax.swing.JOptionPane;

public class HollowRightAngledTriangle {


public static void main(String[] args) {
String character = JOptionPane.showInputDialog("Enter a character:");
int depth = Integer.parseInt(JOptionPane.showInputDialog("Enter the
height/depth of the triangle:"));

System.out.println("You have entered the character " + character);

for (int i = 0; i < depth; i++) {


for (int j = 0; j <= i; j++) {
if (j == 0 || j == i || i == depth - 1) {
System.out.print(character);
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

Question 5

import javax.swing.JOptionPane;

public class NestedStudentMarksWithMethodsAndParameters {


public static void main(String[] args) {
int numStudents = Integer.parseInt(JOptionPane.showInputDialog("Enter the
number of students:"));
int numModules = Integer.parseInt(JOptionPane.showInputDialog("Enter the
number of modules:"));

processMarks(numStudents, numModules);
}

public static void processMarks(int numStudents, int numModules) {


for (int i = 1; i <= numStudents; i++) {
int totalMarks = 0;

for (int j = 1; j <= numModules; j++) {


int mark = Integer.parseInt(JOptionPane.showInputDialog("Enter the
mark for Student " + i + " - Module " + j + ":"));
totalMarks += mark;
}

double averageMark = (double) totalMarks / numModules;

System.out.println("Total Marks for Student " + i + ": " + totalMarks);


System.out.println("Average Mark for Student " + i + ": " +
averageMark);
System.out.println("End Of Student " + i + "'s Academic Record");
}
}
}

You might also like