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

HABBILING, Reniel M

The document contains 5 Java code examples that demonstrate: 1) Calculating total resistance for resistors in series or parallel based on user input 2) Determining highest, middle, lowest numbers and performing other operations on 3 user-input numbers 3) Classifying a year as leap year, century year, or regular year based on user-input year 4) Mapping a numeric grade to a letter grade based on ranges 5) Calculating parking charge based on hours parked and rates
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

HABBILING, Reniel M

The document contains 5 Java code examples that demonstrate: 1) Calculating total resistance for resistors in series or parallel based on user input 2) Determining highest, middle, lowest numbers and performing other operations on 3 user-input numbers 3) Classifying a year as leap year, century year, or regular year based on user-input year 4) Mapping a numeric grade to a letter grade based on ranges 5) Calculating parking charge based on hours parked and rates
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

HABBILING, Reniel M.

September 16, 2019


BSECE 1 1:30-3:30 MWF
1. Read three resistors and the connection desired, compute the total resistance based on desired connection.
public class Prob11_Resistor_HABBILING {
public static void main(String[] args) {
System.out.print("Enter type of connection: ");
String con= Keyboard.readString();
if(con.equalsIgnoreCase("series")){
System.out.print("Enter Resistor 1: ");
double r1= Keyboard.readDouble();
System.out.print("Enter Resistor 2: ");
double r2= Keyboard.readDouble();
System.out.print("Enter Resistor 3: ");
double r3= Keyboard.readDouble();
double rT= r1+r2+r3;
System.out.printf("The total resistance is %.2f",rT);
}
else{
if(con.equalsIgnoreCase("parallel")){
System.out.print("Enter Resistor 1: ");
double r1= Keyboard.readDouble();
System.out.print("Enter Resistor 2: ");
double r2= Keyboard.readDouble();
System.out.print("Enter Resistor 3: ");
double r3= Keyboard.readDouble();
double rT= 1/((1/r1)+(1/r2)+(1/r3));
System.out.println("-------------------------------");
System.out.printf("The total resistance is %.2f",rT);
}
else
System.out.print("INVALID connection");
}
}
}
2. Input three numbers (X,Y,Z). Determine and output the
following:
a) highest number (HN)
b) middle number (MN)
c) smallest number (SN)
d) the numbers in ascending order
e) the numbers in descending order
f) only the odd numbers
g) the sum of the 3 numbers
h) the average of the 3 numbers.
import java.util.*;
public class Prob12_ThreeNumbers_HABBILING {
public static void main(String[] args) {
Scanner reniel = new Scanner(System.in);
System.out.print("Enter x: ");
int x = reniel.nextInt();

System.out.print("Enter y: ");
int y = reniel.nextInt();
System.out.print("Enter z: ");
int z = reniel.nextInt();
int HN,MN,LN;
if(x>y&&y>z){
HN = x;
MN = y;
LN = z;
System.out.printf("Highest number: %d",HN);

System.out.printf("\nMiddle number: %d",MN);

System.out.printf("\nLowest number: %d",LN);

System.out.printf("\nAscending Order: %d %d %d",LN,MN,HN);

System.out.printf("\nDescending Order: %d %d
%d",HN,MN,LN);
}
else
if(x>z&&z>y){
HN = x;
MN = z;
LN = y;
System.out.printf("Highest number: %d",HN);
System.out.printf("\nMiddle number: %d",MN);
System.out.printf("\nLowest number: %d",LN);
System.out.printf("\nAscending Order: %d %d %d",LN,MN,HN);
System.out.printf("\nDescending Order: %d %d %d",HN,MN,LN);
}
else
if(y>x&&x>z){
HN = y;
MN = x;
LN = z;
System.out.printf("Highest number: %d",HN);
System.out.printf("\nMiddle number: %d",MN);
System.out.printf("\nLowest number: %d",LN);
System.out.printf("\nAscending Order: %d %d %d",LN,MN,HN);
System.out.printf("\nDescending Order: %d %d %d",HN,MN,LN);
}
else
if(y>x&&x>z){
HN = y;
MN = x;
LN = z;
System.out.printf("Highest number: %d",HN);
System.out.printf("\nMiddle number: %d",MN);
System.out.printf("\nLowest number: %d",LN);
System.out.printf("\nAscending Order: %d %d %d",LN,MN,HN);
System.out.printf("\nDescending Order: %d %d %d",HN,MN,LN);
}
else
if(y>z&&z>x){
HN = y;
MN = x;
LN = z;
System.out.printf("Highest number: %d",HN);
System.out.printf("\nMiddle number: %d",MN);
System.out.printf("\nLowest number: %d",LN);
System.out.printf("\nAscending Order: %d %d %d",LN,MN,HN);
System.out.printf("\nDescending Order: %d %d %d",HN,MN,LN);
}
System.out.print("\nOdd Number(s): ");
if(x%2==1){
System.out.print(x+" ");
}
if(y%2==1){
System.out.print(y+" ");
}
if(z%2==1){
System.out.print(z);
}
int sum = x + y + z;
double ave = (double)sum/3;
System.out.println("\nThe sum is "+sum);
System.out.printf("The average is %5.2f",ave);
}
}

3. A leap year is a year divisible by 4 unless it is a century year, in which case it must be divisible by 100. Read a
year and output a message whether the year is a leap year, century year or otherwise.
public class Prob13_Year_HABBILING {
public static void main(String[] args) {
System.out.print("Enter Year: ");
int yr = Keyboard.readInt();
if(yr%4==0){
if(yr%100==0){
System.out.print("Century Year");
}
else
System.out.print("Leap Year");
}
else
System.out.print("Regular Year");
}
}
4. A professor converts numeric grades to letter grades in the following way:
93 – 99 Excellent
87 – 92 Very Good
80 – 86 Good
70 – 79 Fair
65 - 69 Poor
Read a numeric grade and output the equivalent description.
public class Prob15_Grades_HABBILING {
public static void main(String[] args) {
System.out.print("Enter Grade: ");
double gr = Keyboard.readDouble();
if(gr>=93&&gr<=99){
System.out.println("EXCELLENT");
}
else
if(gr>=87&&gr<=92){
System.out.println("VERY GOOD");
}
else
if(gr>=80&&gr<=86){
System.out.println("GOOD");
}
else
if(gr>=70&&gr<=79){
System.out.println("FAIR");
}
else
if(gr>=65&&gr<=69){
System.out.println("POOR");
}
else
System.out.println("INVALID Grade");
}
}

5. Parking charge per hour at SMBC underground parking is as follows:


P 35.00 - minimum charge for 4 hours parking or less,
P 15.00/hr.- additional charge in excess of 4 hours parking,
P 250.00 - maximum charge
Input : number of hours (HRS) the vehicle was parked.
Output : parking charge (CHARGE).

public class Prob18_Parking_HABBILING {


public static void main(String[] args) {
System.out.print("Number of hours the vehicle was parked: ");
double HRS = Keyboard.readDouble();
if(HRS<4){
System.out.println("Parking Charge: Php 35.00");
}
else{
double Ch= 35+15*(HRS-4);
if(Ch>250){
System.out.println("Parking Charge: Php250.00");
}
else
System.out.printf("Parking Charge: Php%.2f",Ch);
}
}
}

6. A telephone area code is a three digit number. The first of which is either 1 or 9, the second is any number among
5, 6, 7, 8, and 9. The third digit is any non zero number. Read a three digit number and checks to see if it is a valid
code. Output a message saying so.
public class Prob19_TelephoneCode_HABBILING {
public static void main(String[] args) {
System.out.print("Enter Telephone Area Code: ");
int num = Keyboard.readInt();
int a = num/100;
int b = (num%100)/10;
int c = (num%100)%10;
if(num<=999){
if(a==1||a==9){
if(b>4&&b<10){
System.out.println("VALID
Telephone Code");
}
else

System.out.println("INVALID Telephone Code");


}
else
System.out.println("INVALID Telephone Code");
}
else
System.out.println("INVALID Telephone Code");
}
}

7. The ABC Electric Company bases its electricity charges on two rates. Customers are charged P 30.117 per
kilowatt-hour (KWH) for the first 400 KWH used in a month, and P25.23 for all KWH used thereafter. Read in
the consumption and output amount to be charged to the customer.
public class Prob21_Electricity_HABBILING {
public static void main(String[] args) {
System.out.print("Enter consumption in KWH: ");
double KWH = Keyboard.readDouble();
if(KWH<=400){
double Ch = 30.117*KWH;
System.out.printf("Amount Charged: %.2f",Ch);

}
else{
double Ch = (30.117*400)+(25.23*(KWH-400));
System.out.printf("Amount Charged: %2f",Ch);
}
}
}

8. A certain store has the following scheme:


Commodity Code:
A - commodities are discounted by 15%
B - commodities are taxed by 10%
C - commodities are charged as priced
Read the commodity code, quantity of the commodities bought and the unit price and output the amount to be
paid by the customer.
public class Prob22_CommodityCode_HABBILING {
public static void main(String[] args) {
System.out.print("Commodity Code: ");
String cc = Keyboard.readString();
if(cc.equalsIgnoreCase("a")){
System.out.print("Quantity Bought: ");
int q = Keyboard.readInt();
System.out.print("Unit Price: ");
int p = Keyboard.readInt();
double total = p*q*0.85;
System.out.printf("Total Amount: %.2f",total);
}
else
if(cc.equalsIgnoreCase("b")){
System.out.print("Quantity Bought: ");
int q = Keyboard.readInt();
System.out.print("Unit Price: ");
int p = Keyboard.readInt();
double total = p*q*1.10;
System.out.printf("Total Amount: %.2f",total);
}
else
if(cc.equalsIgnoreCase("c")){
System.out.print("Quantity Bought: ");
int q = Keyboard.readInt();
System.out.print("Unit Price: ");
int p = Keyboard.readInt();
double total = p*q;
System.out.printf("Total Amount: %.2f",total);
}
else
System.out.println("INVALID Code");
}
}

9.
public class Prob15_Grade_Switch_HABBILING {
public static void main(String[] args) {
System.out.print("Enter Grade: ");
int gr = Keyboard.readInt();
switch(gr){
case 65:
case 66:
case 67:
case 68:
case 69:
System.out.println("POOR");
break;
case 70:
case 71:
case 72:
case 73:
case 74:
case 75:
case 76:
case 77:
case 78:
case 79:
System.out.println("FAIR");
break;
case 80:
case 81:
case 82:
case 83:
case 84:
case 85:
case 86:
System.out.println("GOOD");
break;
case 87:
case 88:
case 89:
case 90:
case 91:
case 92:
System.out.println("VERY GOOD");
break;
case 93:
case 94:
case 95:
case 96:
case 97:
case 98:
case 99:
System.out.println("EXCELLENT");
break;
default:
System.out.println("INVALID Grade");
break;
}

}
}
10.

import java.util.Scanner;
public class Prob22_Commodity_Switch_HABBILING {

public static void main(String[] args) {

Scanner reniel = new Scanner(System.in);


System.out.print("Commodity Code: ");
String cc = reniel.nextString();

int q, p;
double total;
switch(cc){
case "a":
case "A":
System.out.print("Quantity Bought: ");
q = Keyboard.readInt();
System.out.print("Unit Price: ");
p = Keyboard.readInt();
total = p*q*0.85;
System.out.printf("Total Amount: %.2f",total);
break;
case "b":
case "B":
System.out.print("Quantity Bought: ");
q = Keyboard.readInt();
System.out.print("Unit Price: ");
p = Keyboard.readInt();
total = p*q*1.10;
System.out.printf("Total Amount: %.2f",total);
break;
case "c":
case "C":
System.out.print("Quantity Bought: ");
q = Keyboard.readInt();
System.out.print("Unit Price: ");
p = Keyboard.readInt();
total = p*q;
System.out.printf("Total Amount: %.2f",total);
break;
default:
System.out.println("INVALID Code");
}
}
}

You might also like