Vous êtes sur la page 1sur 5

/**

*MUHAMMAD HISYAM BIN MALANG


* Matric no: 1822907
* Lab #2 Section #1
*/
Exercise 1

public
class
Exercise1
{

public static void main(String[] args) {

int month = (int)((Math.random() * 12) + 1);

switch (month)

case 1: System.out.println("January"); break;

case 2: System.out.println("February"); break;

case 3: System.out.println("March"); break;

case 4: System.out.println("April"); break;

case 5: System.out.println("May"); break;

case 6: System.out.println("June"); break;

case 7: System.out.println("July"); break;

case 8: System.out.println("August"); break;

case 9: System.out.println("September"); break;

case 10: System.out.println("October"); break;

case 11: System.out.println("November"); break;

case 12: System.out.println("December");

}}
Exercise 2

import java.util.Scanner;

public class Exercise_02 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the weight of the package: ");

double weight = input.nextDouble();

if (weight > 50)

System.out.println("The package cannot be shipped.");

else

double costPerPound;

if (weight > 0 && weight <= 1)

costPerPound = 3.5;

else if (weight <= 3)

costPerPound = 5.5;

else if (weight <= 10)

costPerPound = 8.5;

else //if (weight <= 20)

costPerPound = 10.5;

System.out.println("Shipping cost of package is $" +

costPerPound * weight);

}
}

}
Exercise 3

import java.util.Scanner;

public class Exercise3 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int positives = 0;

int negatives = 0;

int count = 0;

double total = 0;

System.out.print("Enter an integer, the input ends if it is 0: ");

int number = input.nextInt();

if (number == 0) {

System.out.println("No numbers are entered except 0");

System.exit(1);

while (number != 0) {

if (number > 0)

positives++;

else
negatives++;

total += number;

count++;

number = input.nextInt();

double average = total / count;

System.out.println(

"The number of positive is " + positives +

"\nThe number of negatives is " + negatives +

"\nThe total is total " + total +

"\nThe average is " + average);

Vous aimerez peut-être aussi