Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Program Name: BCS

Course Code: CSC 2513

Course Name: Programming Fundamentals

Assignment: Second

Date of Submission: 17th May 2021

Submitted by: Submitted To:

Student Name: Udit Kumar Mahato Faculty Name: Mr. Prakash Chandra

IUKL ID: 042002900006 Department: PO Office (BCS)

Semester: Second Semester

Intake: Sept 2020

1|Page
1. Write JAVA program with the following:

- Create an integer array of size 5

- Input 5 integers from user

- Calculate sum of all integers in array

- Sort the integers in ascending order

Source Code:-

import java.util.Scanner;

class ArrayNum {
public static void main(String[] args) {
//initiated sum
int sum=0;
//initialized temp to store the value for temporary period
int temp;
//Created array NumStore
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 numbers");
int NumStore[] = new int[5];

//Store to store 5 int in the array numStore;


for(int i=0;i<5;i++){
NumStore[i]=sc.nextInt();
// now calculate the sum
sum=sum+NumStore[i];
}
System.out.println("Sum of all numbers : "+sum);

//loop to print array values before sorting


System.out.println("Before sorting:");
for(int i=0; i<5;i++){
System.out.print(NumStore[i]+" ");
}
//loop to sort the value of array

for(int i=0;i<5;i++){
for(int j=i+1;j<5;j++){
if(NumStore[i]>NumStore[j]){
temp=NumStore[i];
NumStore[i]=NumStore[j];
NumStore[j]=temp;
}

2|Page
}
}
System.out.println("\nAfter sorting:");

//loop to print value after sorting in ascending order


for(int i=0;i<5;i++){
System.out.print(NumStore[i]+" ");
}
}
}
//Sole copyright to Code by Udit Kumar Mahato

Output:

2. A college calculates total marks obtained in 5 subjects by a student and based on their total
percentage score the result is calculated as per the table 1(B).

Your program should include the following information:

- Ask the user to enter the marks obtained in 5 subjects and store it in an array.

- Calculate the total marks obtained and percentage score

3|Page
- Based on the percentage score, display the RANK.

- Ask the user to enter name of student

- Use java file handling to store the name and rank in a file “score.txt”

Source Code:

import java.util.Scanner;
import java.io.*;
class College {
public static void main(String[] args) throws FileNotFoundException {
//intialization of object
float MarksSum=0;
String Result;
Scanner sc=new Scanner(System.in);
PrintWriter out = new PrintWriter("score.txt");
out.printf("##########RESULT##########");
//creation of array to store marks
float MarkStore[]= new float[5];
System.out.println("Enter the marks obtained in 5 subjects: ");

//Loop to store value in array


for(int i=0;i<5;i++){
MarkStore[i]=sc.nextFloat();
MarksSum=MarksSum+MarkStore[i];
}
System.out.println("Total Marks Obtained : "+MarksSum);
float Percentage=(MarksSum/500)*100;
System.out.println("Percentage Score : "+Percentage+"%");
//loops for division
if(Percentage<29.9){
Result="FAIL";
System.out.println("Division : "+Result);
}
else if(Percentage>=30 && Percentage<=59.9){
Result="SECOND";
System.out.println("Division : "+Result);
}
else if(Percentage>=60 && Percentage<=79.9){
Result="FIRST ";
System.out.println("Division : "+Result);
}
else{

4|Page
Result="DISTINCTION";
System.out.println("Division : "+Result);
}
Scanner res=new Scanner(System.in);
System.out.print("Enter the name of Student : " );
String StudentName=res.nextLine();
//now lets print in txt.file
out.printf("\n"+"Name : "+StudentName);
out.printf("\n"+"Division : "+Result.toLowerCase());
sc.close();
out.close();
}

Output:

Text file:

3. Write java program based on the following class diagram:

5|Page
Following are the details of the method used:

- The constructor Product(String,double,double) should initialize name,price,stock value.

- The method getName() should return name of product.

- The method getPrice() should return Price of product.

-The method IsInStock() should return TRUE if stock value is more than 50kg otherwise false.
(NOTE: consider stock value is kg.).

Your program should contain the following:

- Create a file called ‘product.txt’ using java file handling to record the information in following
format

Source code:
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
class ProductStock {
String name;
double price;
double StockValue;
//Constructor
ProductStock(String name, double price, double StockValue){
this.name = name;
this.price = price;
this.StockValue = StockValue;
}

6|Page
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public boolean IsInStock(){
double stock = StockValue;
if (stock>50){
return true;
}
else {
return false;
}
}
}
// main class
class Product {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter product name, price and stock value: ");
String nam = sc.nextLine();
double pri = sc.nextDouble();
double sto = sc.nextDouble();
//allocate memory
ProductStock P = new ProductStock(nam,pri,sto);
//to print in product file
PrintWriter outF = new PrintWriter("product.txt");
outF.printf("####### Product Information #######\n");
outF.printf("product Name: " + P.getName() + "\n");
outF.printf("product Price: " + P.getPrice() + "\n");
outF.printf("Availability: " + P.IsInStock() + "\n");
outF.close();

}
}

Output:

7|Page
Text file:

8|Page

You might also like