MIT 11053: Fundamentals of Programming Tutorial for Chapter 02
By: Prof. S. Sabraz Nawaz, Professor in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL
Exercise 01
Write a Java program that adds two numbers and displays the total.
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
System.out.println("Total is: Rs." + total);
}
}
Exercise 02
The Total Sales of a company is one million rupees. The Sales for Ampara district is 34% of the Total Sales. Write a
Java program that calculates the Sales Amount of Ampara District and displays.
public class SalesPrediction
{
public static void main(String[] args)
{
double totalSales = 1000000.0; // Total sales
double amparaSales; // Ampara district sales
// Calculate Ampara district sales.
amparaSales = totalSales * 0.34;
// Display the prediction.
System.out.println("Ampara district sales prediction: Rs." +
eastCoastSales);
}
}
Exercise 03
Write a Java program that holds details of a person’s name, age and his annual income and displays them on the
screen.
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 = 43;
MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz
1
MIT 11053: Fundamentals of Programming Tutorial for Chapter 02
By: Prof. S. Sabraz Nawaz, Professor in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL
annualIncome = 217000.0;
// Display a message.
System.out.println("My name is " + name + ", my age is " + age +
" and I hope to earn Rs." +annualIncome+ " per
year.");
}
}
Exercise 04
public class PersonalInformation
{
public static void main(String[] args)
{
// The "\n" is an escape 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
Write a Java program that holds First Name, Middle Name and Last Name of a person and displays the Full Name.
public class PrintName
{
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
System.out.println("Name: " + firstName + " " + middleName + " " +
lastName);
}
}
Exercise 06 (Constants)
There are 6000 shares in a company. The price of a share is 21.77 rupees. Commission given is 2%. Write a Java
program that calculate stock cost and commission on that and total cost of stock, display these results on screen.
public class StockCommission
{
public static void main(String[] args)
MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz
2
MIT 11053: Fundamentals of Programming Tutorial for Chapter 02
By: Prof. S. Sabraz Nawaz, Professor in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL
{
// Constants
final int NUM_SHARES = 6000; // 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
stockCost = STOCK_PRICE * NUM_SHARES;
commission = stockCost * COMM_PERCENT;
total = stockCost + commission;
System.out.println("Stock cost: Rs." + stockCost);
System.out.println("Commission: Rs." + commission);
System.out.println("Total: Rs." + total);
}
}
Exercise 07
Write a Java program that takes three scores and displays the average on screen.
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
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter test score #1: ");
test1 = keyboard.nextDouble();
System.out.print("Enter test score #2: ");
test2 = keyboard.nextDouble();
System.out.print("Enter test score #3: ");
test3 = keyboard.nextDouble();
MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz
3
MIT 11053: Fundamentals of Programming Tutorial for Chapter 02
By: Prof. S. Sabraz Nawaz, Professor in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL
average = (test1 + test2 + test3) / 3.0;
System.out.println("Test average: " + average);
}
}
Exercise 08
A hotel charges 6.75% as Tax on all its bills. In addition to this, 15% is added as Tips for the service. Write a Java
program that takes Meals Charge as input, calculate Tax and Tip amount and displays the Total Bill on the screen.
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
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the charge for the meal: ");
mealCharge = keyboard.nextDouble();
tax = mealCharge * TAX_RATE;
tip = mealCharge * TIP_PERCENT;
total = mealCharge + tax + tip;
System.out.println("Meal Charge: Rs." + mealCharge);
System.out.println("Tax: Rs." + tax);
System.out.println("Tips: Rs." + tip);
System.out.println("Total: Rs." + total);
}
}
A FEW MORE PRACTICE EXAMPLES
EXAMPLE 01 (Using Scanner class and Constants)
import java.util.Scanner; // Needed for the Scanner class
public class SalesTax
{
MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz
4
MIT 11053: Fundamentals of Programming Tutorial for Chapter 02
By: Prof. S. Sabraz Nawaz, Professor in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL
public static void main(String[] args)
{
final double STATE_RATE = 0.04; // State tax rate
final double COUNTY_RATE = 0.02; // County tax rate
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
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the purchase amount: ");
purchase = keyboard.nextDouble();
stateTax = purchase * STATE_RATE;
countyTax = purchase * COUNTY_RATE;
totalTax = stateTax + countyTax;
totalCost = purchase + totalTax;
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);
}
}
EXAMPLE 02
public class StockTransaction
{
public static void main(String[] args)
{
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);
MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz
5
MIT 11053: Fundamentals of Programming Tutorial for Chapter 02
By: Prof. S. Sabraz Nawaz, Professor in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL
// 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 received 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);
}
}
EXAMPLES 03
import java.util.Scanner; // Needed for the Scanner class
public class StringManipulator
{
public static void main(String[] args)
{
MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz
6
MIT 11053: Fundamentals of Programming Tutorial for Chapter 02
By: Prof. S. Sabraz Nawaz, Professor in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL
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));
}
}
MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz
7