0% found this document useful (0 votes)
70 views7 pages

Exercise 01: MIT 11053, Lab Sheet #02

This document contains 10 programming exercises demonstrating basic Java concepts like variables, constants, input/output, and calculations. Each exercise contains a Java class with a main method that declares and assigns variables, performs calculations, and prints output to the console. The exercises cover topics such as summing numbers, sales predictions, name/age output, personal information output, initials, stock transactions, surveys, test averages, restaurant bills, and sales tax calculations.

Uploaded by

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

Exercise 01: MIT 11053, Lab Sheet #02

This document contains 10 programming exercises demonstrating basic Java concepts like variables, constants, input/output, and calculations. Each exercise contains a Java class with a main method that declares and assigns variables, performs calculations, and prints output to the console. The exercises cover topics such as summing numbers, sales predictions, name/age output, personal information output, initials, stock transactions, surveys, test averages, restaurant bills, and sales tax calculations.

Uploaded by

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

MIT 11053, Lab sheet #02

Exercise 01
public class SumOfTwoNumbers
{
public static void main(String[] args)
{
int number1, number2, total; // Variables

number1 = 62; // Assign the first number


number2 = 99; // Assign the second number
total = number1 + number2; // Calculate the sum
}
}

Exercise 02
public class SalesPrediction
{
public static void main(String[] args)
{
double totalSales = 4600000.0; // Total sales
double amparaSales; // Ampara district sales

// Calculate Ampara district sales.


amparaSales = totalSales * 0.62;

// Display the prediction.


System.out.println("Ampara district sales prediction: $" + eastCoastSales);
}
}

Exercise 03
public class NameAgeAnnualIncome
{
public static void main(String[] args)
{
String name; // To hold a name
int age; // To hold an age
double annualIncome; // To hold annualIncome

// Store values in the String object and variables.


name = "Sams SaNa";
age = 33;
annualIncome = 108000.0;

// Display a message.
System.out.println("My name is " + name + ", my age is " + age +
" and I hope to earn Rs." +annualIncome+ " per year.");
}
}

Fundamentals of Programming by S. Sabraz Nawaz Page 1


MIT 11053, Lab sheet #02
Exercise 04
public class PersonalInformation
{
public static void main(String[] args)
{
// The "\n" is an escapte character used to create a new line
System.out.println("Sabraz Nawaz\n" + "124 Main Street\n" +
"Sainthamaruthu-01, EP 32280\n" + "(077)777-7777\n" +
"Information Systems");
}
}
Exercise 05
public class NameAndInitials
{
public static void main(String[] args)
{
String firstName = "Sabraz"; // First name
String middleName = "Nawaz"; // Middle name
String lastName = "Samsudeen"; // Last name

char firstInitial; // To hold the first initial


char middleInitial; // To hold the middle initial
char lastInitial; // To hold the last initial

// Get the initials.


firstInitial = firstName.charAt(0);
middleInitial = middleName.charAt(0);
lastInitial = lastName.charAt(0);

// Display the contents of the variables.


System.out.println("Name: " + firstName + " " + middleName + " " +
lastName);
System.out.println("Initials: " + firstInitial + middleInitial + lastInitial);
}
}

Exercise 06 (Constants)
public class StockCommission
{
public static void main(String[] args)
{
// Constants
final int NUM_SHARES = 600; // Number of shares
final double STOCK_PRICE = 21.77; // Price per share
final double COMM_PERCENT = 0.02; // Commission percentage

// Variables
double stockCost; // Cost of stock
double commission; // Commission
double total; // Total of the transaction

// Calculate the stock cost.


stockCost = STOCK_PRICE * NUM_SHARES;

// Calculate the commission.


commission = stockCost * COMM_PERCENT;

Fundamentals of Programming by S. Sabraz Nawaz Page 2


MIT 11053, Lab sheet #02
// Calculate the total.
total = stockCost + commission;

// Display the results.


System.out.println("Stock cost: Rs." + stockCost);
System.out.println("Commission: Rs." + commission);
System.out.println("Total: Rs." + total);
}
}

Exercise 07
public class EnergyDrinks
{
public static void main(String[] args)
{
// Constant for the number surveyed
final int NUM_SURVEYED = 12467;

// Constant for the percentage who purchase


// energy drinks
final double ENERGY_DRINKERS_PCT = 0.14;

// Constant for the percentage of energy drinkers


// who prefer citrus flavor
final double PREFER_CITRUS_PCT = 0.64;

// Variables
double energyDrinkers; // Number of energy drinkers
double preferCitrus; // Number that prefer citrus

// Calculate the number of energy drinkers.


energyDrinkers = NUM_SURVEYED * ENERGY_DRINKERS_PCT;

// Calculate the number of energy drinkers that


// prefer citrus flavor.
preferCitrus = energyDrinkers * PREFER_CITRUS_PCT;

// Display the results.


System.out.println("We surveyed " + NUM_SURVEYED + " people.");
System.out.println("Out of those surveyed, approximately " +
energyDrinkers + " purchase at least one " +
"energy drink per year.");
System.out.println("Approximtely " + preferCitrus + " of those " +
"prefer citrus flavored energy drinks.");
}
}

Exercise 08
import java.util.Scanner; // Needed for the Scanner class

public class TestAverage


{
public static void main(String[] args)
{
double test1; // Test score #1
double test2; // Test score #2
double test3; // Test score #3
double average; // Average of the test scores

Fundamentals of Programming by S. Sabraz Nawaz Page 3


MIT 11053, Lab sheet #02
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get the first test score.


System.out.print("Enter test score #1: ");
test1 = keyboard.nextDouble();

// Get the second test score.


System.out.print("Enter test score #2: ");
test2 = keyboard.nextDouble();

// Get the third test score.


System.out.print("Enter test score #3: ");
test3 = keyboard.nextDouble();

// Calculate the average.


average = (test1 + test2 + test3) / 3.0;

// Display the average.


System.out.println("Test average: " + average);
}
}

Exercise 09
import java.util.Scanner;

public class RestaurantBill


{
public static void main(String[] args)
{
// Constants
final double TAX_RATE = 0.0675; // Tax rate
final double TIP_PERCENT = 0.15; // Tip percentage

// Variables
double mealCharge; // To hold the meal charge
double tax; // To hold the amount of tax
double tip; // To hold the tip amount
double total; // To hold the total charge

// Create a Scanner object for keyboard input.


Scanner keyboard = new Scanner(System.in);

// Get the charge for the meal.


System.out.print("Enter the charge for the meal: ");
mealCharge = keyboard.nextDouble();

// Calculate the tax.


tax = mealCharge * TAX_RATE;

// Calculate the tip.


tip = mealCharge * TIP_PERCENT;

// Calculate the total.


total = mealCharge + tax + tip;

// Display the results.


System.out.println("Meal Charge: $" + mealCharge);

Fundamentals of Programming by S. Sabraz Nawaz Page 4


MIT 11053, Lab sheet #02
System.out.println("Tax: $" + tax);
System.out.println("Tip: $" + tip);
System.out.println("Total: $" + total);
}
}

Exercise 10 (Using Scanner class and Constants)


import java.util.Scanner; // Needed for the Scanner class

public class SalesTax


{
public static void main(String[] args)
{
// Constants
final double STATE_RATE = 0.04; // State tax rate
final double COUNTY_RATE = 0.02; // County tax rate

// Variables
double purchase; // Amount of purchase
double stateTax; // Amount of state sales tax
double countyTax; // Amount of county sales tax
double totalTax; // Total sales tax
double totalCost; // Total cost of the purchase

// Create a Scanner object for keyboard input.


Scanner keyboard = new Scanner(System.in);

// Get the amount of the purchase.


System.out.print("Enter the purchase amount: ");
purchase = keyboard.nextDouble();

// Calculate the state sales tax.


stateTax = purchase * STATE_RATE;

// Calculate county sales tax.


countyTax = purchase * COUNTY_RATE;

// Calculate the total sales tax.


totalTax = stateTax + countyTax;

// Calculate the total purchase cost.


totalCost = purchase + totalTax;

// Display the results.


System.out.println("Purchase amount: Rs." + purchase);
System.out.println("State tax: Rs." + stateTax);

System.out.println("County tax: Rs." + countyTax);


System.out.println("Total tax: Rs." + totalTax);
System.out.println("Total cost: Rs." + totalCost);
}
}

Fundamentals of Programming by S. Sabraz Nawaz Page 5


MIT 11053, Lab sheet #02
Exercise 11
public class StockTransaction
{
public static void main(String[] args)
{
// Named constants
final int NUM_SHARES = 1000; // Number of shares
final double PURCHASE_PRICE = 32.87; // Purchase price per share
final double SELLING_PRICE = 33.92; // Selling price per share
final double BROKER_COMM_RATE = 0.02; // Broker commission rate

// Calculate the cost of the stock (without the


// broker's commission) and store the result
// in stockPurchase.
double stockPurchase = (NUM_SHARES * PURCHASE_PRICE);

// Calculate the broker's commission on the purchase and


// store the result in purchaseComm.
double purchaseComm = stockPurchase * BROKER_COMM_RATE;

// Calculate the total amount SaNa paid for the stock plus the
// broker's commission and store the result in amountPaid.
double amountPaid = stockPurchase + purchaseComm;

// Calculate the amount SaNa sold the stock for and store
// the result in stockSale.
double stockSale = NUM_SHARES * SELLING_PRICE;

// Calculate the broker's commission on the sale and


// store the result in sellingComm.
double sellingComm = (NUM_SHARES * SELLING_PRICE) *
BROKER_COMM_RATE;

// Calculate the amount that SaNa actually recieved after


// selling the stock and paying his broker the sales
// commission, and store the result in amountRecieved.
double amountRecieved = stockSale - sellingComm;

// Calculate the amount of profit or loss, and store the


// result in profitOrLoss. If a profit was made, the amount
// will be positive. If a loss was incurred, the amount
// will be negative.
double profitOrLoss = amountRecieved - amountPaid;

// Display the results.


System.out.println("SaNa paid Rs." + stockPurchase +
" for the stock.");
System.out.println("SaNa paid his broker a commission of Rs." +
purchaseComm + " on the purchase.");
System.out.println("So, SaNa paid a total of Rs." + amountPaid + "\n");

System.out.println("SaNa sold the stock for Rs." + stockSale);


System.out.println("SaNa paid his broker a commission of Rs." +
sellingComm + " on the sale.");
System.out.println("So, SaNa recieved a total of Rs." + amountRecieved + "\n");
System.out.println("SaNa's profit or loss: Rs." + profitOrLoss);
}
}

Fundamentals of Programming by S. Sabraz Nawaz Page 6


MIT 11053, Lab sheet #02
Exercise 12(String Manipulation)
public class NameAndInitials
{
public static void main(String[] args)
{
String firstName = "Sabraz"; // First name
String middleName = "Nawaz"; // Middle name
String lastName = "Samsudeen"; // Last name

char firstInitial; // To hold the first initial


char middleInitial; // To hold the middle initial
char lastInitial; // To hold the last initial

// Get the initials.


firstInitial = firstName.charAt(0);
middleInitial = middleName.charAt(0);
lastInitial = lastName.charAt(0);

// Display the contents of the variables.


System.out.println("Name: " + firstName +
" " + middleName + " " +
lastName);
System.out.println("Initials: " + firstInitial +
middleInitial + lastInitial);
}
}

Exercise 13
import java.util.Scanner; // Needed for the Scanner class
public class StringManipulator
{
public static void main(String[] args)
{
String city; // To hold user input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name of your favorite city: ");
city = keyboard.nextLine();

System.out.println("Number of characters: " + city.length());


System.out.println(city.toUpperCase());
System.out.println(city.toLowerCase());
System.out.println(city.charAt(0));
}
}

Fundamentals of Programming by S. Sabraz Nawaz Page 7

You might also like