Department of Computer Sciences: Subject Submitted To Submitted by Date of Submission

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Department of Computer

Sciences
Assignment No.01

 Subject OOP with Java

 Submitted To Mam Zainab Malik

 Submitted By Asif Ali

 Date of Submission 26-03-2021


Q.1. Writ a program to calculate total cost of three items. The price and
quantity of item taking from the user and display the total cost.
Source code:

package program1;
import java.util.Scanner;
public class Totalcost {

public static void main(String[] args) {


//intiallzing
int quantity[]=new int[3];
int price[]=new int[10];
int total=0;
int totalCost=0;
//creating object to getting the numbers of item
Scanner and=new Scanner(System.in);
System.out.println("Enter:How many iteam do you want: ");
int n=and.nextInt();
//for loop is used for taking the prices of items
for (int i=0; i<n; i++){//
System.out.print("Enter the Prices : " );
price[i]=and.nextInt();
}
//for loop is used for taking the quantities of items
for (int i=0; i<n; i++){
System.out.print("Enter the Quantities : ");
quantity[i]=and.nextInt();
}
//to calculate the total cost of items
for(int i=0;i<n;i++){
total=quantity[i]* price[i];
totalCost+=total;
}
System.out.println("The Total cost is :" + totalCost);

}
}
OUTPUT SCREEN SHOT::
question 1 .mp4
VIDEO:
Q.2. Program to take three integer values from user and calculate the
average and display the output.

Source Code:
import java.util.Scanner;

public class Threeinteger{

public static void main(String[] args) {


System.out.println(" Please Enter the Three Value ");
int a, b, c;
double avg, sum;
Scanner scan=new Scanner(System.in);
System.out.println(" we tell Average and Sum : ");
//taking the three values
System.out.println("Enter the first number");
a=scan.nextInt();
System.out.println("Enter the second number");
b=scan.nextInt();
System.out.println("Enter the Third number");
c=scan.nextInt();
//the sum is calculated
sum=a+b+c;
System.out.println("The Sum is : " + sum);
//the average is calculated
avg=sum/3;
System.out.println("The Average is : " + avg);
}

OUTPUT SCREEN SHOT::


question 2.mp4
VIDEO:
Write a program to take temperature in Fahrenheit and convert into
Celsius and display the output.
Source Code:
package question3;

import java.util.Scanner;

/**

* @author asif ali

*/

public class Tempconvert {

public static void main(String[] args) {

//intiallize the Fahrenheit and Celsius

double ftemp;

double Celsius;

System.out.print("Enter the temperature in Fahrenheit : ");

//creating object

Scanner scan=new Scanner(System.in);

//for taking the fahrenheit temperature from the user

ftemp=scan.nextDouble();

//the formula of celsius is

Celsius =((5.0*(ftemp-32.0))/9.0);

System.out.println("The Temperature in Celsius is :" + Celsius);

}
OUTPUT SCREEN SHOT::

question 3.mp4
VIDEO:
Q.4. Write a program that will first print a menu as (Marks: 10) To enter marks of
ITCP press 1 To enter marks of OOP press 2 To enter marks for TBW press 3 To
enter marks for DBMS press 4 To exit press 52 After displaying this menu,
program should take input (1, 2, 3, 4 or 5) from user. After that display message
according to selected subject choice e.g. if user presses 1 then next message
should be How many quizzes of ITCP? Or if user presses 2 then message should be
as follow How many quizzes in OOP? and so on. After that receive total number
of quizzes in a specific subject from user and then ask user to enter marks of each
quiz (if quizzes are 5 you need to take 5 inputs: marks of 5 quizzes). After taking
marks print the average score in that subject and print the menu again. Program
should repeat this entire process (from displaying menu to average calculation)
again and again till user enters 5.

SOURCE CODE:
//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public


public class Menu {
//void main
public static void main (String[] args)
{
//declare float
float a,sum = 0;
int choice;
//Declare input as scanner
Scanner input = new Scanner(System.in);

//Take inputs
System.out.println("1\t To enter marks of ITCP press.");
System.out.println("2\t To enter marks of OOP press.");
System.out.println("3\t To enter marks for TBW press.");
System.out.println("4\t To enter marks for DBMS press.");
System.out.println("5\t To exit press ");
String s = input.next();
choice = s.charAt(0);

//add a switch statement


switch(choice)
{
case '1':
System.out.println("How many quizzes of ITCP?:");
a = input.nextFloat();

System.out.println("ist quiz:");
a = input.nextFloat();
System.out.println("2nd quiz:");
a = input.nextFloat();
System.out.println("3rd quiz:");
a = input.nextFloat();
System.out.println("4rd quiz:");
a = input.nextFloat();
System.out.println("5rd quiz:");
a = input.nextFloat();
sum = a+a+a+a+a;
break;
case '2':
System.out.println("How many quizzes of OOP?:");
a = input.nextFloat();
System.out.println("ist quiz:");
a = input.nextFloat();
System.out.println("2nd quiz:");
a = input.nextFloat();
System.out.println("3rd quiz:");
a = input.nextFloat();
System.out.println("4rd quiz:");
a = input.nextFloat();
System.out.println("5rd quiz:");
a = input.nextFloat();
sum = a+a+a+a+a;
break;
case '3':
System.out.println("How many quizzes of TBM?:");
a = input.nextFloat();
System.out.println("ist quiz:");
a = input.nextFloat();
System.out.println("2nd quiz:");
a = input.nextFloat();
System.out.println("3rd quiz:");
a = input.nextFloat();
System.out.println("4rd quiz:");
a = input.nextFloat();
System.out.println("5rd quiz:");
a = input.nextFloat();
sum = a+a+a+a+a;
break;
case '4':
System.out.println("How many quizzes of DBMS?:");
a =input.nextFloat();
System.out.println("ist quiz:");
a = input.nextFloat();
System.out.println("2nd quiz:");
a = input.nextFloat();
System.out.println("3rd quiz:");
a = input.nextFloat();
System.out.println("4rd quiz:");
a = input.nextFloat();
System.out.println("5rd quiz:");
sum = a+a+a+a+a;
break;
default:
System.out.println("Invalid choiceS");
}

System.out.println("Score = "+sum);
}
}

SCREEN SHOT::
question 4.mp4
VIDEO::
Q.5. Write a program in java to generate grocery bill (containing items purchased, quantity of each item, price per item
and total amount). For this purpose, program should ask user how many types of items purchased. Then it should ask for
name, quantity and price per item for each item. If there are total 5 different types of items then program should ask for
name, quantity and price per item five times and all should be saved appropriately. (Marks: 10) Hint: Use three arrays one
for item names, one for quantity and one for price per item as: Item Name 0 1 2 3 4 5 Egg Tissue Box Soap Mushrooms
Olives Detergent Quantity 0 1 2 3 4 5 12 5 4 2 1 1 Price Per Item 0 1 2 3 4 5 5 40 30 120 150 230 After taking this input,
program should calculate total amount and should display output as: Name Quantity Price per Item Egg 12 5 Tissue Box 5
40 Soap 4 30 Mushrooms 2 120 Olives 1 150 Detergent 1 230 Total Amount 575

SOURCE CODE::

package program5;
// FOR GETTING USER INPUT WE NEED TO IMPORT JAVA METHOD LIKE SCANNER
import java.util.Scanner;
public class bill {
// MAIN FUNCTION
public static void main(String[] args) {
//creatng string arrays objects for names
String Name[]=new String[40];
//creating arrays objects for quantities and prices
int quan[]=new int[10];
int price[]=new int[10];
int total[]=new int[20];
//intiallizing
int types_q;
int total_price=0;
Scanner sc=new Scanner(System.in);
System.out.println("\tHOW MANY ITEMS DO YOU WANT TO PURCHASE? : ");
System.out.println("**..............................................** : ");
//to getting the number of items from user
types_q=sc.nextInt();
System.out.println("ENTER THE Name OF ITEMS : ");
//for loop is used for taking the names of items
for(int i=0;i<=types_q; i++){
Name[i]=sc.nextLine();
}
System.out.println("ENTER PRICE Of Each ITEM : ");
//for loop is used for taking the prices of items
for(int i=1;i<=types_q; i++){
price[i]=sc.nextInt();
}
System.out.println("Enter The Quantity Of Each ITEM: ");
//for loop is used for taking the quantities of items from the user
for(int i=1;i<=types_q; i++){
quan[i]=sc.nextInt();
}
for(int i=1;i<=types_q;i++){
total[i]=quan[i]* price[i];
//calculating and displaying the total cost
System.out.println("\tName OF items : " + Name[i] + "\t\t Quantity of ITEM : " + quan[i]+
"\t\tprice of items : " + price[i] +"\tTotal price is : " + total[i]);
total_price+=total[i];
}
System.out.println("\nTotal price is : \t\t\t\t\t\t\t\t \t\t\t\t" + total_price);
}
}

SCREEN SHOT::
question 5.mp4
VIDEO::

OTHER method Question 5:


Source code:
package asif;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Product {
// properties
private final String pname;
private final int qty;
private final double price;
private final double totalPrice;

// constructor
Product(String pname, int qty,
double price, double totalPrice) {
this.pname = pname;
this.qty = qty;
this.price = price;
this.totalPrice = totalPrice;
}

public String getPname() {


return pname;
}
public int getQty() {
return qty;
}
public double getPrice() {
return price;
}
public double getTotalPrice() {
return totalPrice;
}
// displayFormat
public static void displayFormat() {
System.out.print(
"\nName Quantity Price Total Price\n");
}

// display
public void display() {
System.out.format("%-9s %8d %10.2f %10.2f\n",
pname, qty, price, totalPrice);
}
}

public class ShoppingBill {


public static void main(String[] args) {

// variables
String productName = null;
int quantity = 0;
double price = 0.0;
double totalPrice = 0.0;
double overAllPrice = 0.0;
char choice = '\0';

// create Scanner class object


Scanner scan = new Scanner(System.in);

List<Product> product = new ArrayList<>();

do {
// read input values
System.out.println("Enter product details,");
System.out.print("Name: ");
productName = scan.nextLine();
System.out.print("Quantity: ");
quantity = scan.nextInt();
System.out.print("Price (per item): ");
price = scan.nextDouble();

// calculate total price for that product


totalPrice = price * quantity;

// calculate overall price


overAllPrice += totalPrice;

// create Product class object and add it to the list


product.add( new Product(
productName, quantity, price, totalPrice) );

// ask for continue?


System.out.print("Want to add more item? (y or n): ");
choice = scan.next().charAt(0);

// read remaining characters, don't store (no use)


scan.nextLine();
} while (choice == 'y' || choice == 'Y');

// display all product with its properties


Product.displayFormat();
product.forEach(p -> {
p.display();
});
// overall price
System.out.println("\nTotal Price = " + overAllPrice);

// close Scanner
scan.close();
}

SCREEN SHOT:
question 5 part
b.mp4
VIDEO ::

You might also like