Exercise
Saturday, September 23, 2023 2:29 PM
public class test2 {
public static void main(String[] args) {
String name = " Joe Mahoney ";
int age = 26;
double earn = 10000.0;
System.out.println("My name is " + name + " , my age is " + age + " and I
hope to earn " + earn + " per year.");
}
public class test4 {
public static void main(String[] args) {
String firstname = "Oeur";
String middlename = "Thea";
String lastname = "Makny";
char firstInitial = firstname.charAt(0);
char middleIntitial = middlename.charAt(0);
char lastnameIntitial = lastname.charAt(0);
System.out.println(" First name is: " + firstname);
System.out.println(" Middle name is: " + middlename);
System.out.println(" Last name is: " + lastname);
System.out.println(" Initials " + firstInitial + middleIntitial +
lastnameIntitial);
}
3
public class test5 {
public static void main(String[] args) {
String name = "Oeur Theamakny";
String address = "Phnom Penh";
int telephone = 012345667;
String major = "Data science and AI engineering";
System.out.print(name + "\n" + address + "\n" + telephone + "\n" +
major);
4
public class test6 {
public static void main(String[] args) {
double eastcoast = 0.62;
double generate = 4.6000000;
double predit = eastcoast * generate;
OOC Page 1
double predit = eastcoast * generate;
System.out.println(predit);
}
}
5
public class test7{
public static void main(String[] args) {
double squareFeet = 389767;
double acresPerSquareFoot = 1 / 43560.0;
double acres = squareFeet * acresPerSquareFoot;
System.out.println("A tract of land with " + squareFeet + " square feet
has " + acres + " acres.");
}
}
6
import java.util.Scanner;
public class test8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double purchaseamount;
double sale_tax = 0.04;
double country_saletax = 0.02;
System.out.println("Enter the purchase amount:");
purchaseamount = scanner.nextDouble();
double state_of_sale_tax = purchaseamount * sale_tax;
double the_country_saletax = purchaseamount * country_saletax;
double total_saletax = state_of_sale_tax + country_saletax;
double total_of_sale = purchaseamount + total_saletax;
System.out.println(state_of_sale_tax);
System.out.println(the_country_saletax);
System.out.println(total_of_sale);
System.out.println(total_saletax);
}
}
import java.util.Scanner;
import javax.sound.midi.Sequencer.SyncMode;
public class test9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int cookies_eat;
int caloriespercookie = 300 /10;
System.out.println("Enter cookies that you eat");
cookies_eat = scanner.nextInt();
int toatalcalories = cookies_eat * caloriespercookie;
System.out.println("The calories of cookies that you = " +
OOC Page 2
System.out.println("The calories of cookies that you = " +
toatalcalories);
}
8
import java.util.Scanner;
import javax.sound.midi.Sequencer.SyncMode;
import javax.swing.plaf.synth.SynthScrollPaneUI;
public class test10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double mile_driven;
double gallons_of_gas_used ;
System.out.println("Enter the number of miles driven :");
mile_driven = scanner.nextDouble();
System.out.println("Enter the of gas you used :");
gallons_of_gas_used = scanner.nextDouble();
double MPG = mile_driven / gallons_of_gas_used;
System.out.println(" Your car's miles-per-gallo" + MPG);
9
import java.util.Scanner;
public class test11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double test1_score;
double test2_score;
double test3_score;
System.out.println("Enter test 1 score :");
test1_score = scanner.nextDouble();
System.out.println("Enter the test 2 score :");
test2_score = scanner.nextDouble();
System.out.println("Enter the test 3 score");
test3_score = scanner.nextDouble();
double aver_score = (test1_score + test2_score + test3_score) /3;
System.out.println("Your average score:" + aver_score);
}
}
10
import java.util.Scanner;
public class test12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double retail_price;
double percent_profit =0.4;
System.out.println("Enter retail price:");
retail_price = scanner.nextDouble();
double profit = retail_price * percent_profit;
System.out.println("The amount of profit earn for that product, and
display the result " + profit );
}
}
11
OOC Page 3
11
import java.util.Scanner;
public class test13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String favorite_city;
System.out.println("Enter your favorite city");
favorite_city = scanner.nextLine();
System.out.println(favorite_city.length());
System.out.println(favorite_city.toUpperCase());
System.out.println(favorite_city.toLowerCase());
System.out.println(favorite_city.charAt(0));
}
12
import java.util.Scanner;
public class test14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double cost_meal;
System.out.println("Enter the charge of meal:");
cost_meal = scanner.nextDouble();
double tax_amount = cost_meal * 0.675;
double total_meal = cost_meal + tax_amount;
double total_tip = total_meal * 0.2;
double totalbill = total_meal + total_tip;
System.out.println("Tax amount: " + tax_amount);
System.out.println("Total price of meal :" + total_meal);
System.out.println("Amount of tip to pay : " + total_tip);
System.out.println("Total bill" + totalbill);
}
14
public class test15 {
public static void main(String[] args) {
double shares = 600;
double price = 21.77;
double commision_rate =0.02;
double stock_alone = shares * price;
double the_commision = stock_alone * commision_rate;
double the_total = stock_alone + the_commision;
System.out.println("The amount paid for the stock alone : " +
stock_alone);
System.out.println("THe amount of the commission : " + the_commision);
System.out.println("The total amount paid: " + the_total);
}
}
13
import java.util.Scanner;
OOC Page 4
import java.util.Scanner;
public class test16 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int male;
System.out.println("The number of male in the class are:");
male = scanner.nextInt();
int female;
System.out.println("The number of female in the class are : ");
female = scanner.nextInt();
int total_student = male + female;
double percentage_male = ((double) male / total_student) * 100;
double percentage_female = ((double)female / total_student) * 100;
System.out.println("The percentage of male student in the class :" +
percentage_male + "%");
System.out.println("The percentage of female student in the class: " +
percentage_female + "%");
}
15
public class test17 {
public static void main(String[] args) {
int totalCustomers = 12467;
double energyDrinkCustomerPercentage = 0.14;
double energyDrinkCustomers = totalCustomers *
energyDrinkCustomerPercentage;
double citrusFlavoredCustomerPercentage = 0.64;
double citrusFlavoredCustomers = energyDrinkCustomers *
citrusFlavoredCustomerPercentage;
// Display the results
System.out.println("Approximate number of customers who purchase one
or more energy drinks per week: " + energyDrinkCustomers);
System.out.println("Approximate number of customers who prefer
citrus-flavored energy drinks: " + citrusFlavoredCustomers);
}
}
17
import java.util.Scanner;
public class test18 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name;
int age;
String city;
String college;
String proffession;
String type_animal;
String pet_name;
System.out.println("Enter your name :");
name = scanner.nextLine();
OOC Page 5
name = scanner.nextLine();
System.out.println("Enter your age:");
age = scanner.nextInt();
System.out.println("Enter the city that you live :");
city = scanner.nextLine();
System.out.println("Enter your college that you study");
college = scanner.nextLine();
System.out.println("Enter your profession :");
proffession = scanner.nextLine();
System.out.println("Enter the type of animal :");
type_animal = scanner.nextLine();
System.out.println("Enter your pet name :");
pet_name = scanner.nextLine();
System.out.println("There once was a person named " + name + " who lived
in " + city + ". At the age of " + age + ", " + name + " went to college at " +
college + ". " + name + " graduated and went to work as a " + proffession + ".
Then, " + name + " adopted a(n) " + type_animal + " named " + pet_name + ". They
both lived happily ever after!");
18
public class test19 {
public static void main(String[] args) {
int numOfShares = 1000;
double purchase_Price_PerShare = 32.8;
double salePricePerShare = 33.92;
double purchaseAmount = numOfShares * purchase_Price_PerShare;
double purchaseCommission = purchaseAmount * 0.02;
double saleAmount = numOfShares * salePricePerShare;
double saleCommission = saleAmount * 0.02;
double profitOrLoss = saleAmount - purchaseAmount -
purchaseCommission - saleCommission;
// Display the results
System.out.println("Amount paid for the stock: $" + purchaseAmount);
System.out.println("Commission paid when buying the stock: $" +
purchaseCommission);
System.out.println("Commission paid when selling the stock: $" +
saleCommission);
System.out.println("Profit or loss: $" + profitOrLoss);
}
}
OOC Page 6