Slip14
Q1) Write a program to accept a number from the user, if number is zero then throw user defined exception “Number is 0” otherwise check whether no is prime or not (Use static keyword). [10 marks]
//Slip14_1
import java.util.*;
class NumberException extends Exception
{
NumberException()
{
System.out.println("Number is 0");
}
}
class Slip14_1
{
public static void main( String args[] )
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number ");
int n=sc.nextInt();
try
{
if(n==0)
throw new NumberException();
else
{
int i,m=0,flag=0;
m=n/2;
if(n==0||n==1)
System.out.println(n+" is not prime number");
else
{
for(i=2;i<=m;i++)
{
if(n%i==0)
{
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
}
if(flag==0)
System.out.println(n+" is prime number");
}
}
catch(NumberException e){}
}
}
Q2) Write a Java program to create a Package “SY” which has a class SYMarks (members – ComputerTotal, MathsTotal, and ElectronicsTotal). Create another package TY which has a class TYMarks (members – Theory, Practicals). Create ‘n’ objects of Student class (having rollNumber, name, SYMarks and TYMarks). Add the marks of SY and TY computer subjects and calculate the Grade (‘A’ for >= 70, ‘B’ for >= 60 ‘C’ for >= 50, Pass Class for > =40 else‘FAIL’) and display the result of the student in proper format. [20 marks]
//SYmarks.java
package sy;
public class SYmarks
{
int computertotal;
int mathstotal;
int electronicstotal;
public SYmarks()
{
computertotal=0;
mathstotal=0;
electronicstotal=0;
}
public SYmarks(int c,int m,int e)
{
this.computertotal=c;
this.mathstotal=m;
this.electronicstotal=e;
}
}
//TYmarks.java
package ty;
public class TYmarks
{
public int theory;
public int practicals;
public TYmarks()
{
theory=0;
practicals=0;
}
public TYmarks(int t,int p)
{
this.theory=t;
this.practicals=p;
}
}
//main program
import sy.*;
import ty.*;
import java.io.*;
public class Student
{
int roll_no;
String name;
SYmarks sym;
TYmarks tym;
String Grade;
public Student(int r, String na,int c_t,int m_t,int e_t,int t1,int p,String g)
{
roll_no=r;
name=na;
sym=new SYmarks(c_t,m_t,e_t);
tym=new TYmarks(t1,p);
Grade=g;
}
/*********************************************************************/
//Displaying Roll No,Name , Marks and Grade
public void show(int c_t,int m_t,int e_t,int t1,int p,String g )
{
System.out.println("Roll No: "+roll_no);
System.out.println("Name :"+name);
System.out.println("SY Computer marks :"+c_t);
System.out.println("SY Maths marks :"+m_t);
System.out.println("SY Electronic marks :"+e_t);
System.out.println("TY theory marks :"+t1);
System.out.println("TY practical marks :"+p);
System.out.println("Grade :"+g);
}
public static void main(String[] args)throws IOException
{
String g;
int n;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter how many students");
n=Integer.parseInt(br.readLine());
Student s[]=new Student[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter Roll No");
int r=Integer.parseInt(br.readLine());
System.out.println("Enter Name");
String na=br.readLine();
System.out.print("Enter SY Computer marks");
int c_t=Integer.parseInt(br.readLine());
System.out.print("Enter SY Maths marks");
int m_t=Integer.parseInt(br.readLine());
System.out.print("Enter SY Electronics marks");
int e_t=Integer.parseInt(br.readLine());
System.out.print("Enter TY Theory marks");
int t1=Integer.parseInt(br.readLine());
System.out.print("Enter TY Practical marks");
int p=Integer.parseInt(br.readLine());
double avrg=(c_t+t1+p)/3;
//Calculate grade
if(avrg>=70.0)
{
g="A";
}
else if(avrg<70.0 && avrg>=60.0)
{
g="B";
}
else if(avrg<60.0 && avrg>=50.0)
{
g="C";
}
else if(avrg<50.0 && avrg>=40)
{
g="Pass";
}
else
g="Fail";
s[i]=new Student(r,na,c_t,m_t,e_t,t1,p,g);
// Display Details
s[i].show(c_t,m_t,e_t,t1,p,g);
}
}
}
Comments
Post a Comment