Professional Documents
Culture Documents
CSC 2513 - Second Assignment
CSC 2513 - Second Assignment
Assignment: Second
Student Name: Udit Kumar Mahato Faculty Name: Mr. Prakash Chandra
1|Page
1. Write JAVA program with the following:
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];
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:");
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).
- Ask the user to enter the marks obtained in 5 subjects and store it in an array.
3|Page
- Based on the percentage score, display the RANK.
- 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: ");
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:
5|Page
Following are the details of the method used:
-The method IsInStock() should return TRUE if stock value is more than 50kg otherwise false.
(NOTE: consider stock value is kg.).
- 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