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

OOP Assignment 1 (Omer Ishaq)

This document contains 5 Java programs as assignments submitted by Omer Ishaq to his instructor Ms. Zainab Malik. The programs include: 1) Calculating total cost of items by taking price and quantity from the user, 2) Calculating average of 3 integers, 3) Converting temperature from Fahrenheit to Celsius, 4) Calculating average quiz marks for a selected subject, and 5) Generating a grocery bill by taking item names, quantities, and prices from the user. Each program shows the input, output, and code.

Uploaded by

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

OOP Assignment 1 (Omer Ishaq)

This document contains 5 Java programs as assignments submitted by Omer Ishaq to his instructor Ms. Zainab Malik. The programs include: 1) Calculating total cost of items by taking price and quantity from the user, 2) Calculating average of 3 integers, 3) Converting temperature from Fahrenheit to Celsius, 4) Calculating average quiz marks for a selected subject, and 5) Generating a grocery bill by taking item names, quantities, and prices from the user. Each program shows the input, output, and code.

Uploaded by

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

OBJECT ORIENTED PROGRAMMING

ASSIGNMENT NO: 1

SUBMITTED TO: Ms Zainab Malik

SUBMITTED BY: Omer Ishaq

CLASS: BSCS-1(Bridging)

ASSIGNMENT TOPIC: Basic Java Programming

SUBMISSION DATE: 26th March 2021

NATIONAL UNIVERSITY OF MODERN LANGUAGES


Department of Computer Science
PROGRAM#1: TAKE THE PRICE AND QUANTITY OF ITEMS FROM THE
USER AND DISPLAY THE TOTAL COST.

Input:

import java.util.Scanner; // importing scanner class from java.util package


public class OOPtotalCost {
public static void main (String[] args) {
Scanner scan=new Scanner (System.in); // creating scanner class object
System.out.println ("Enter the quantity of items");
int num=scan.nextInt( ); //initializing variable num
double cost[ ]; // declaring an array cost with type double
cost=new double[num];
for (int i=0; i<num; i++) // loop for entering values in elements of array “cost”
{
System.out.println ("Enter cost of item no " + (i+1));
cost[i] = scan.nextDouble( ); //entering prices in consecutive elements of cost array
} // for loop closing
double total=0; // initializing variable “total”
for (int i=0; i<num; i++) // loop for calculating total cost of items
{
total = total + cost[i];
} // for loop closing
System.out.println ("Total cost of items is " + total); // displaying total cost
} // main closing
} //class closing
PROGRAM#1: TAKE THE PRICE AND QUANTITY OF ITEMS FROM THE
USER AND DISPLAY THE TOTAL COST.

Output:
PROGRAM#2: TAKE THREE INTEGER VALUES FROM THE USER AND
CALCULATE THE AVERAGE. THEN DISPLAY THE OUTPUT.

Input:

import java.util.Scanner; // importing scanner class from java.util package


public class OOPAvg {
public static void main(String[] args) {
Scanner scan=new Scanner (System.in); // creating scanner class object
System.out.println ("Enter 1st integer value");
int num=scan.nextInt( ); // initializing variables and assigning an input value to it
System.out.println ("Enter 2nd integer value");
int num1=scan.nextInt( );
System.out.println ("Enter 3rd integer value");
int num2=scan.nextInt( );
System.out.print ("Average of integers " +num); // displaying output of average and
System.out.print (" " +num1+ " and " +num2+ " = "); // explicit type casting its result
System.out.print ( (double)(num+num1+num2)/3 );
} // main closing
} // class closing
PROGRAM#2: TAKE THREE INTEGER VALUES FROM THE USER AND
CALCULATE THE AVERAGE. THEN DISPLAY THE OUTPUT.

Output:
PROGRAM#3: TAKE TEMPERATURE IN FAHRENHEIT AND CONVERT INTO
CELSIUS AND DISPLAY THE OUTPUT.

Input:

import java.util.Scanner; // importing scanner class from java.util package


public class OOpTaskfahrenheitCelsius {
public static void main (String[] args) {
Scanner scan=new Scanner (System.in); // creating scanner class object
System.out.println ("Enter temperature in Fahrenheit" );
float f=scan.nextFloat( ); // initializing variable and assigning an input value to it
System.out.println ("Temperature in Celsius = " + (f-32)*5/9); // using formula for result
} // main closing
} // class closing

Output:
PROGRAM#4: CALCULATE AVERAGE MARKS FOR QUIZZZES IN
THE SELECTED SUBJECT.

Input:

import java.util.Scanner; // importing scanner class from java.util package


public class QuizAverage{
int sub; // declaring an instance variable
public int averageMarks( ){ // creating a function
String subject="sub";
Scanner scan=new Scanner (System.in); // creating scanner class object
If (sub==1) // if else if statement is used for multiple options
subject="ITCP"; // switch statement is not used because of break statement
else if (sub==2)
subject="OOP";
else if (sub==3)
subject="TBW";
else if (sub==4)
subject="DBMS";
else
return 0; // in case of options other than 1-4 ,nothing is returned by function
System.out.println ( "How many quizzes of " + subject);
int quiz=scan.nextInt(); // initializing quiz variable
int marks[]; // declaring an array named “marks”
marks=new int[quiz];
for (int i=0; i<quiz; i++)
{

PROGRAM#4: CALCULATE AVERAGE MARKS FOR QUIZZZES IN


THE SELECTED SUBJECT.

Input (Continuation):

System.out.println ("Enter marks for quiz” + (i+1));


marks[i]=scan.nextInt();
} // for loop closing
int total=0; // variable initialized here, otherwise there will be issue of scope
for (int i=0; i<quiz; i++) // using loop for calculating total marks
{
total=total+marks[i];
}
System.out.println (“Average marks for " +quiz+ " quizzes in "+subject+ " = " +
(double) total/quiz);
return 0; // return statement is used here otherwise there will be error
}
public static void main(String[] args) {
Scanner scan1=new Scanner (System.in); // creating scanner class object
System.out.println ("To enter marks for ITCP press\t\t" +1);
System.out.println ("To enter marks for OOP press \t\t" +2);
System.out.println ("To enter marks for TBW press \t\t" +3);
System.out.println ("To enter marks for DBMS press\t\t" +4);
System.out.println ("To exit press \t\t" +5);
QuizAverage obj=new QuizAverage( ); //creating object for accessing instance variable
obj.sub=scan1.nextInt( );
obj.averageMarks( ); // calling averageMarks function using object
}} // closing for main and class respectively

PROGRAM#4: CALCULATE AVERAGE MARKS FOR QUIZZZES IN


THE SELECTED SUBJECT.

Output:
PROGRAM#5: GENERATE GROCERY BILL CONTAINING TYPES OF ITEMS
PURCHASED, QUANTITY OF EACH ITEM, PRICE PER ITEM BY TAKING
INPUT FROM USER AND THEN OUTPUT THE TOTAL AMOUNT.

Input:
import java.util.Scanner; // importing scanner class from java.util package
public class GroceryBill {
public static void main (String[] args) {
Scanner scan=new Scanner (System.in); // creating scanner class object
System.out.println ("How many types of items purchased?");
int type=scan.nextInt();
String item_name[]={"Egg ","Tissue Box","Soap 5","Mushrooms ","Olives
","Detergent 5" }; // static initializing a string type array
int quantity[]=new int[6]; // declaring a 1-D array with 6 elements
double priceperitem[]=new double[6]; // declaring an array
for (int i=0; i<6; i++){ // for loop for taking input from user
System.out.println ("Enter quantity of "+item_name[i]);
quantity[i]=scan.nextInt(); // assigning values to elements of array quantity
} // for closing
for (int i=0; i<6; i++){ // loop for storing price per item
System.out.println ("Enter price per item of "+item_name[i]);
priceperitem[i]=scan.nextDouble(); // storing price per item in array elements
} // for closing
System.out.println ("Name\t\t Quantity\t\tPrice per item"); // main heading output
for (int i=0; i<6; i++){ // loop for displaying quantity and price of particular item
System.out.println (item_name[i]+"\t\t"+quantity[i]+"\t\t\t"+priceperitem[i]);
}

PROGRAM#5: GENERATE GROCERY BILL CONTAINING TYPES OF ITEMS


PURCHASED, QUANTITY OF EACH ITEM, PRICE PER ITEM BY TAKING
INPUT FROM USER AND THEN OUTPUT THE TOTAL AMOUNT.

Input (Continuation):
double total=0; //total is initialized here because of scope restrictions
for (int i=0; i<6; i++){ // loop for calculating total amount of grocery bill
total=total+(quantity[i]*priceperitem[i]);
} // for loop closing
System.out.println ("Total Amount = "+total+" Rs"); // output for total input
}} // main and class closing respectively

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy