Sunteți pe pagina 1din 17

Programming in JAVA

CSE1007
Assessment – III
Name:D.Penchal Reddy
Reg.no:17bce0918
Q1) Create a class Student with name, register number and qualifying mark as members.
Define the required input, output methods.
a. Create a static variable classAverage in Student class, and define a static method float
getClassAverage () in Student class. Initialize the classAverage with the value 0 through the
static block and value of classAverage to be updated when an object of Student class is
created.
b. Create n objects of Student class and display the value of classAverage using
getClassAverage() method after each object creation.
c. Finally display details of all students.

CODE:
import java.util.*;
import java.lang.*;
class Student{
String name,regno;
int qm;
static float classAvg;
static int ctr;
static{
classAvg = 0f;
}
public void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter name,regno, Avaerge mark");
name = sc.next();
regno = sc.next();
qm = sc.nextInt();
classAvg+=qm;
ctr++;
}
public static float getClassAverage(){
return classAvg/ctr;
}
public void display(){
System.out.print("Name :"+ name );
System.out.print(" Register No. :"+ regno );
System.out.print(" Average mark :"+ qm );
System.out.println("\n");
}
}
class StudentMain{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of students");
int n = sc.nextInt();
float avg;
Student arr[] = new Student[n];
for(int i=0;i<n;i++){
arr[i] = new Student();
arr[i].input();
avg = arr[i].getClassAverage();
System.out.println("Class Average = " + avg);
}
System.out.println("\nDisplaying Student Details\n");
for(int i=0;i<n;i++){
arr[i].display();
}
}
}
OUTPUT:

Q2) Create a class GeometricObject which contains String color and Boolean filled.
Define methods getArea and getPerimeter.
a. Design a class named Triangle that extends GeometricObject. The class contains
three double data fields named side1, side2, and side3. A constructor that creates a
triangle with the specified side1, side2, side3, its color and filled detail.
b. Design a class named Rectangle that extends GeometricObject. The class contains
two double data fields named length and width. A constructor that creates a rectangle
with the specified length, width, color and filled detail.
c. Override getArea and getPerimeter according to the subclass requirements. Define
a democlass in Java which creates objects for subclasses and demonstrates all
methods.

CODE:
import java.util.*;
class GeometricObject{
String color;
boolean filled;
void getArea(){}
void getPerimeter(){}
}
class Triangle extends GeometricObject{
double s1,s2,s3;
Triangle(double s1,double s2,double s3,String color,boolean
filled){
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.color = color;
this.filled = filled;
}
void getArea(){
double s = 0.5*(s1+s2+s3);
System.out.println("Area of Triangle :"+ (Math.sqrt(s*(s-s1)*(s-s2)*(s-s3) ) ) );
}
void getPerimeter(){
System.out.println("Perimeter of Triangle :"+ (s1+s2+s3) );
}
void display(){
System.out.println("Side 1:"+s1);
System.out.println("Side 2:"+s2);
System.out.println("Side 3:"+s3);
System.out.println("Color :"+color);
System.out.println("Filled :"+filled);
}
}
class Rectangle extends GeometricObject{
double l,w;
Rectangle(double l,double w,String color,boolean filled){
this.l = l;
this.w = w;
this.color = color;
this.filled = filled;
}
void getArea(){
System.out.println("Area of Rectangle :"+ (l*w) );
}
void getPerimeter(){
System.out.println("Perimeter of Rectangle :"+ ( 2 * (l+w)
) );
}
void display(){
System.out.println("Length :"+l);
System.out.println("Width :"+w);
System.out.println("Color :"+color);
System.out.println("Filled :"+filled);
}
}
class GeomMain{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter sides of triangle, color and filled");
double s1,s2,s3;
s1=sc.nextDouble();
s2=sc.nextDouble();
s3=sc.nextDouble();
String Tcolor; boolean Tfilled;
Tcolor = sc.next();
Tfilled = sc.nextBoolean();
Triangle t = new Triangle(s1,s2,s3,Tcolor,Tfilled);
System.out.println("Properties of triangle");
t.display();
t.getArea();
t.getPerimeter();
System.out.println("Enter length and width, color and filled");
double l,w;
l=sc.nextDouble();
w=sc.nextDouble();
String Rcolor; boolean Rfilled;
Rcolor = sc.next();
Rfilled = sc.nextBoolean();
Rectangle r = new Rectangle(l,w,Rcolor,Rfilled);
System.out.println("Properties of rectangle");
r.display();
r.getArea();
r.getPerimeter();
}
}

OUTPUT:
3. Define a package to implement the following:
a. Create a class called as Coordinate which stores x and y coordinate values.
b. Develop constructors to initiate the coordinates based on the parameter passed.
c. Design a method distance() to find the distance between two coordinates.
d. Design method print() to print the members of the Coordinate class in the “(x,y)”
format. e. Develop a demo class to test above processes
Ans:
CODE:
import java.io.*;
class MyException extends Exception{
public MyException(String s){
super(s);
}
}
class Demo2{
public static void main(String... args){
Console c=System.console();
String reg;
long mn;
try{
reg=c.readLine("Enter registration number: ");
if (reg.length()!=9)
throw new MyException("Length of registration number should be 9");

for(int i=0;i<9;i++){
if
(!Character.isDigit(reg.charAt(i))&&!Character.isLetter(reg.charAt(i)))
throw new MyException("Registration number
should be alphanumeral!");
}
mn=Long.parseLong(c.readLine("Enter mobile number: "));
String a=mn+"";
if(a.length()!=10)
throw new MyException("Length of mobile number
should be 10");
System.out.println("Registration Number and Mobile Number
accepted!");

}catch(NumberFormatException e){

numerals"); System.out.println("Exception Message: Mobile number should


only contain
}catch(MyException e){
System.out.println("Exception Message: "+e.getMessage());
}
}
}

Q4) Write a program in Java to raise exception for data validation and typo error.
a. Read the Register Number and Mobile Number of a student. If the Register
Number does not contain exactly 9 characters or if the Mobile Number does not
contain exactly 10 characters, throw an IllegalArgumentException.
b. If the Mobile Number contains any character other than a digit, raise a
NumberFormatException.
c. If the Register Number contains any character other than digits and alphabets,
throw a NoSuchEl
ANS)
CODE:
import java.util.*;
import java.util.regex.*;
import java.lang.*;
public class Regex{
public static void main(String[] args)
{
int a=0,b=0;
String regNo,mobNo;
Scanner in=new Scanner(System.in);
System.out.println("Enter the registration number: ");
regNo=in.nextLine();
try{if(regNo.length()!=9)
{
a=1;
throw new IllegalArgumentException("Length of Registration number should be
9");
}}
catch(Exception e)
{
System.out.println(e);}
try{if(!Pattern.matches("[0-9]*[a-z]*[A-Z]*[0-9]*[a-z]*[A-Z]*",regNo))
{
a=1;
throw new NoSuchElementException("Registration number should have only
numbers and alphabets");
}}
catch(Exception e){
System.out.println(e);
}
System.out.println("Please enter the mobile number: ");
mobNo=in.nextLine();
try{
if(mobNo.length()!=10)
{
b=1;
throw new IllegalArgumentException("Lengths do not match");
}}
catch(Exception e)
{
System.out.println(e);
}
try{if(!Pattern.matches("[0-9]+",mobNo))
{
b=1;
throw new NumberFormatException("mobile number should contain only
numbers!!");
}}
catch(Exception e){
System.out.println(e);
}
if(a==1)
System.out.println("Invalid registration number");
else if(a==0)
System.out.println("Valid registration number");
if(b==1)
System.out.println("Invalid mobile number");
else if(b==0)
System.out.println("Valid mobile number");
}}
OUTPUT1:

OUTPUT2:

Q5)Develop a java program to design a RoadRunner game, The player wins the
game if he plays 100 levels and earns 100 coins in within 5 life.Create a class
RoadRunner with datamembers - name, coins, level, life. To play the game, call
play() which in turn calls run(). Inside the run() count the no of jumps; for every
jump, count 1 coin.Once the count of the coin reaches 100 and life is >0&&life.
a. If the status returned is true, Increase the level, else raise an exceptions
b. If the life=0 then raise an exception.
c. If the player completes 100 levels, Declare win else raise an exception.
CODE:
import java.io.*;
class MyException extends Exception{
public MyException(String s){
super(s);
}
};
class RoadRunner implements Runnable{
String name;
int coins,level,life;
boolean status; public RoadRunner(String a){
name=a;
coins=0;
level=1;
life=5;
status=true;
}
public void play() throws MyException{
run();

if (status==true) level++;
print(); if (level==6)
throw new MyException("You win!");
if (life==0)
throw new MyException("You lose because of no lives");
}
public void run() {
Console c= System.console();
System.out.println("Press 1 for jump, 0 for no jump!");
for(int i=0;i<10;i++){
try{
int j=Integer.parseInt(c.readLine("Enter your move: "));
if(j!=1&&j!=0)
throw new MyException("Remaining lives: "+(--
life));
if(j==1){
coins++;
if(coins%10==0)
status=true;

}
}catch(NumberFormatException e){
status=false;
life--;
System.out.println("Remaining lives: "+(life));
break;
}catch(MyException e){
status=false;
System.out.println(e.getMessage());
break;
}
}
}
public void print(){
System.out.println("Name: "+name+" Coins: "+coins+" Lives:
"+life+" Level: "+level);
}
}
class Demo{
public static void main(String... args){
int opt=0;
Console c = System.console();
RoadRunner game = new RoadRunner(c.readLine("Enter player name:
"));
do{
try{
System.out.println("MENU\n1. Play\n2. Exit");
opt=Integer.parseInt(c.readLine("Enter your option: "));
if(opt==1) game.play();
}
catch(Exception e){
System.out.println(e.getMessage());
break;
}

}while(opt!=2);
}
}
Q7) Design a Java program to list the files of a directory along with size. Also find
the average file size, minimum file size and maximum file size of that directory.
ANS)
CODE:
import java.io.File; import java.util.*; class Demo{
public static void main(String... args){
String files[];
File f = new File("D:/Studies/Sem-4/Java/Lab","2"); files = f.list();
Integer length[] = new Integer[files.length];
int length2[] = new int[files.length];
int i=0;
for (String s:files){
File a = new File(s);
length[i]=(int)a.length();
length2[i]=(int)a.length();
System.out.println(s+" Size: "+a.length()+"
bytes");
i++;
}
int min = Collections.min(Arrays.asList(length));
int max = Collections.max(Arrays.asList(length));
int average = Arrays.stream(length2).sum()/files.length;

size: "+average); System.out.println("Minimum size: "+min+" Maximum size:


"+max+" Average

}
}

S-ar putea să vă placă și