24
Object Oriented Programming Lab Practical File P1-Write a Java program that prints all real solutions to the quadratic equation ax2 + bx +c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display a message stating that there are no real solutions. import java.util.*; import java.lang.*; import java.io.*;

Basic java Programs

Embed Size (px)

DESCRIPTION

This document contains some basic java programs .

Citation preview

Page 1: Basic java Programs

Object Oriented Programming Lab

Practical File

P1-Write a Java program that prints all real solutions to the quadratic equation ax2 + bx +c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display a message stating that there are no real solutions.

import java.util.*;import java.lang.*;import java.io.*;class quadratic { public static void main (String ar[]) { try{ int a,b,c;

Page 2: Basic java Programs

Scanner in=new Scanner(System.in); System.out.println("Please enter quadratic coefficent, linear coefficent and constant term in order"); a = in.nextInt(); b = in.nextInt(); c = in.nextInt(); int d = ((b*b)-(4*a*c)); double r= Math.sqrt(d) ; double r1 = (-b+r)/(2*a); double r2 = (-b-r)/(2*a); if (d<0) { System.out.println(); System.out.println ("There are no real solutions."); } else { System.out.println(); System.out.println ("Roots of the given quadratic equation are"+ r1 + " "+"and"+" " + r2 ); } } catch (Exception e) {} } }

Page 3: Basic java Programs
Page 4: Basic java Programs

P2- b)Write a Java program to multiply two given matrices.

import java.util.Scanner; class matrix{ public static void main(String args[]) { int m, n, p, q, sum = 0, c, d, k; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows and columns of first matrix"); m = in.nextInt(); n = in.nextInt(); int first[][] = new int[m][n]; System.out.println("Enter the elements of first matrix"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) first[c][d] = in.nextInt(); System.out.println("Enter the number of rows and columns of second matrix"); p = in.nextInt(); q = in.nextInt(); if ( n != p ) System.out.println("Matrices with entered orders can't be multiplied with each other."); else { int second[][] = new int[p][q]; int multiply[][] = new int[m][q]; System.out.println("Enter the elements of second matrix"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) second[c][d] = in.nextInt(); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[c][k]*second[k][d]; }

Page 5: Basic java Programs

multiply[c][d] = sum; sum = 0; } } System.out.println("Product of entered matrices:-"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) System.out.print(multiply[c][d]+"\t"); System.out.print("\n"); } } }}

Page 6: Basic java Programs

P3- Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that. integer.import java.util.Scanner;

public class prime { public static void main(String [] args) { int n,i=2,c; Scanner sc=new Scanner(System.in); System.out.println("Program to generate the PRIME Series "); System.out.println("Enter any integer"); n=sc.nextInt(); System.out.println("The PRIME Series is ::"); while(i<=n) { c=0; for(int j=1;j<=i;j++) if(i%j==0) c++; if(c==2) System.out.println(" "+i); i++; } }

}

Page 7: Basic java Programs

p-4 Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome.

import java.util.*; class Palindrome{ public static void main(String args[]) { String original, reverse=""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to check if it is a palindrome"); original = in.nextLine(); int length = original.length(); for ( int i = length - 1 ; i >= 0 ; i-- ) reverse = reverse + original.charAt(i); if (original.equals(reverse)) System.out.println("Entered string is a palindrome."); else System.out.println("Entered string is not a palindrome."); }}

Page 8: Basic java Programs

p-5 Write a Java program that reads a file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes.import java.io.*;import java.util.*;class FileTest{ public static void main (String ar[]) { String a; Scanner in=new Scanner(System.in); System.out.println("Please entre the file to be tested."); a=in.nextLine(); File f1 = new File (a); System.out.println("File Exists -"); System.out.println(f1.exists()); System.out.println("File is readable - "); System.out.println(f1.canRead()); System.out.println("File is writable - "); System.out.println(f1.canWrite()); System.out.println("File is a regular file -"); System.out.println(f1.isFile()); System.out.println("File is a directory - "); System.out.println(f1.isDirectory()); System.out.println("Length of file="); System.out.println(f1.length()); }}

Page 9: Basic java Programs
Page 10: Basic java Programs

p-6 Develop an applet that displays a simple message.import java.awt.*;import java.applet.*; /*<applet code=”colapp.class” width=200 height=200> </applet>*/public class colapp extends Applet { public void paint(Graphics g) { g.drawString("I am a simple java applet!!", 10,50); g.setColor(Color.red); g.drawLine(10,10,100,10); g.setColor(Color.blue); g.drawLine(100,10,100,100); g.setColor(Color.green); g.drawLine(10,10,100,100); } }

Page 11: Basic java Programs

p-7 Develop an applet that receives an integer in one text field, and computes its factorial value and returns it in another text field, when the button named “Compute” is clicked. import java.applet.*;import java.awt.*;import java.awt.event.*;

/* <applet code= "FactApplet.class" width=700 height=600> </applet> */public class FactApplet extends Applet implements ActionListener {Label l1,l2;TextField t1,t2;Button b1;public void init(){ l1=new Label("Enter the Number"); add(l1); t1=new TextField(25); add(t1); l2=new Label(" RESULT "); add(l2); t2=new TextField(25); add(t2); b1=new Button("COMPUTE"); add(b1); b1.addActionListener(this);

}public void actionPerformed(ActionEvent ae){ if(ae.getSource()==b1) { int a=Integer.parseInt(t1.getText()); long fact=1;

for(int i=1;i<a+1;i++) { fact=fact*i; } t2.setText(" "+fact); }

}

Page 12: Basic java Programs

}

Page 13: Basic java Programs

p-8 Write a Java program that creates three threads. First thread displays “Good Morning” every one second, the second thread displays “Hi” every two seconds and the third thread displays “Goodnight” every three seconds.class Thread1 extends Thread { public void run() { for(int i=0;i<=10;i++) { try { System.out.println("Thread 1 : "+i+"time Good Morning "); Thread.sleep(2500); } catch(InterruptedException IE) { IE.getMessage(); } }

}}class Thread2 extends Thread{ public void run() { for(int i=0;i<=10;i++) { try { System.out.println("Thread 2: "+i+"time Good night "); Thread.sleep(3500); } catch(InterruptedException IE) { IE.getMessage(); } } }} class Intrest { public static void main(String [] args) { System.out.println("Program to perform multiThreading"); Thread1 t1=new Thread1();

Page 14: Basic java Programs

Thread2 t2=new Thread2(); t1.start(); t2.start(); for(int i=0;i<=10;i++) { try { System.out.println("Main Thread "+i+"time : Hi"); Thread.sleep(1000); } catch(InterruptedException IE) { IE.getMessage(); } }

} }

Page 15: Basic java Programs

p-9Write a program that creates a user interface to perform integer divisions. The user enters two numbers in the textfields, Num1 and Num2. The division of Num1 and Num2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a NumberFormatException. If Num2 were Zero, the program would throw an ArithmeticException Display the exception in a message dialog boximport java.applet.Applet;import java.awt.Button;import java.awt.Label;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;

/* < applet code="ButtonDemo.class" height=400 width=400> </applet> */public class ButtonDemo extends Applet implements ActionListener{ TextField t1,t2,t3; Button b1,b2,b3,b4,b5; Label l1,l2,l3; String msg=""; public void init() { l1=new Label("Enter the First Number"); add(l1); t1=new TextField(20); add(t1); l2=new Label("Enter the Second Number"); add(l2); t2=new TextField(20); add(t2); l3=new Label("Result of Two Numbers"); add(l3); t3=new TextField(20); add(t3); b1=new Button("ADD"); add(b1); b1.addActionListener(this); b2=new Button("SUB"); add(b2); b2.addActionListener(this);

Page 16: Basic java Programs

b3=new Button("MUL"); add(b3); b3.addActionListener(this); b4=new Button("DIV"); add(b4); b4.addActionListener(this); b5=new Button("C"); add(b5); b5.addActionListener(this); } public void actionPerformed(ActionEvent e) { int x=Integer.parseInt(t1.getText()); int y=Integer.parseInt(t2.getText()); if(e.getSource()==b1) { int sum=x+y; t3.setText(" "+sum); showStatus("Text and Button Events Performed"); repaint(); } if(e.getSource()==b2) { int sub=x-y; t3.setText(" "+sub); showStatus("Text and Button Events Performed"); repaint(); } if(e.getSource()==b3) { int mul=x*y; t3.setText(" "+mul); showStatus("Text and Button Events Performed"); repaint(); } try{ if(e.getSource()==b4) { int div=x/y; t3.setText(" "+div); showStatus("Text and Button Events Performed"); repaint(); } } catch (Exception ArithmeticException) { System.out.print("Arithmetic Exception");

Page 17: Basic java Programs

} if(e.getSource()==b5) { t1.setText(" "); t2.setText(" "); t3.setText(" "); showStatus("Text and Button Events Performed"); repaint(); } }}

Page 18: Basic java Programs

p-10)Write a Java program that allows the user to draw lines, rectangles and ovals.import java.applet.Applet;import java.awt.Graphics;

/* <applet code="DrawLines.class" width=400 height=400> </applet> */

public class DrawLines extends Applet { public void paint(Graphics g) { g.drawLine(0,0,100,100); g.drawLine(40,25,250,180); g.drawRect(10,10,60,50); g.fillRect(100,10,60,50); g.drawOval(10,10,60,50); g.fillOval(100,10,60,50);

}

}

Page 19: Basic java Programs