Skip to main content

Posts

Showing posts with the label JAVA

Calculator using switch case in java

//File name--> Calculator.java import java.io.*; class Calculator{ public static void main(String args[])throws Exception { int n1,n2; String choice; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter number1:"); n1=Integer.parseInt(br.readLine()); System.out.println("press \n + for addition \n - for subtraction \n * for multiplication \n / for division: "); choice=br.readLine(); System.out.println("Enter number2:"); n2=Integer.parseInt(br.readLine()); switch(choice) { case "+": System.out.println("Addition: "+n1+"+"+n2+" is:"+(n1+n2)); break; case "-": System.out.println("Subtraction:"+n1+"-"+n2+" is:"+(n1-n2)); break; case "*": System.out.println("Multiplication:"+n1+"*"+n2+" is:"+(n1*n2)); break; case "/...

Fibonacci series in java

//file name--> Fibonacci.java import java.io.*; class Fibonacci{ public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int p=0,c=1,n=p+c,num; System.out.println("Enter number of Series you want:"); num=Integer.parseInt(br.readLine()); for(int i=0;i OUTPUT javac Fibonacci.java java Fibonacci Enter number of Series you want: 10 0 1 1 2 3 5 8 13 21 34

Star patterns in java

//File name--> Pattern.java import java.io.*; class Pattern{ public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your choice to print different pattern(1-5):"); int choice=Integer.parseInt(br.readLine()); System.out.println("Enter Number of lines you want:"); int n=Integer.parseInt(br.readLine()); switch(choice) { case 1: for(int i=1;i < =n;i++) { System.out.println(); for(int j=1;j < =i;j++) System.out.print(" "); int a=i; for(int k=0;k < =n-i;k++) { System.out.print(" "+a); a+=i; } } break; case 2: for(int i=0;i < n;i++) { System.out.println(); for(int j=0;j < i;j++) System.out.print(" "); for(int k=0;k < n-i;k++) ...

Java program to print all real solutions to the quadratic eq. ax2+b+c=0. Read a,b,c values and use the formula (–b+sqrt(b2 4ac))/2a

Quadratic.java import java.io.*; class Quadratic { public static void main(String args[]) throws Exception { double a,b,c; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value of a:"); a=Double.parseDouble(br.readLine()); System.out.println("Enter the value of b:"); b=Double.parseDouble(br.readLine()); System.out.println("Enter the value of c:"); c=Double.parseDouble(br.readLine()); double delta=(b*b)-(4*a*c); if(a==0) { System.out.println("No solution possible"); } else if ( delta == 0 ) { double root = - b / (2 * a); System.out.println("The Real roots are equal:" + root ); } else if(delta OUTPUT javac Quadratic.java java Quadratic Enter the value of a: 2 Enter the value of b: 5 Enter the value of c: 3 The Real roots are unequal -1.0 and -1.5 java Quadratic Enter the value of a: 0 Enter the value of b: 1 Enter...

Area of rectangle in Java

//File name --> Rectangle.java class Rectangle { public static void main(String args[]) { int width=15,height=10; int area=width*height; System.out.println("Area of Rectangle having width "+width+" and height "+height+" is:"+area); } } OUTPUT javac Rectangle.java java Rectangle Area of Rectangle having width 15 and height 10 is:150

(MAY/JUNE- 2012)Programming Solution of Object Oriented Programming with Java (150704)

Q-3(b) (7 marks) Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, Circle. Define one method area() in the abstract class and override this area() in these three subclasses to calculate for specific object i.e. area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle. abstract class Shape{ abstract void area(); } class Circle extends Shape{ double r; Circle(double r) { this.r=r; } void area() { double a=(3.14*r*r); System.out.println("Area of circle having radius "+r+" is:"+a); } } class Rectangle extends Shape{ double w,h; Rectangle(double w,double h) { this.w=w; this.h=h; } void area() { double a=(h*w); System.out.println("Area of rectangle having width "+w+" and height "+h+" is:"+a); } } class Triangle extends Shape{ double b,h; Triangle(double b,double h) { this.b=b; this.h=h; } void area() { double...