Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Untitled

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

package SHARMA;

//20MT1043
//SHARMA

import java.util.Scanner;
public class main {
public static void main(String[] args) {
double r1,r2;{
Scanner input = new Scanner (System.in);
System.out.println("Enter the value of a:");
double a = input.nextDouble();
System.out.println("Enter the value of b:");
double b = input.nextDouble();
System.out.println("Enter the value of c:");
double c = input.nextDouble();
double d = b*b-4.0*a*c;
if (d>0.0) {
r1 = (-b+Math.pow(d, 0.5))/(2.0*a);
r2 = (-b-Math.pow(d, 0.5))/(2.0*a);
System.out.println("The roots are "+r1+"and"+r2);
System.out.println("The roots are real and distinct");
}
else if (d== 0.0) {
r1=r2=b/(2.0*a);
System.out.println("The root is"+r1);
System.out.println("The root is"+r2);
System.out.println("The roots are real and equal");
}
else {
System.out.println("Roots are not real and no real solution");
double x = -b/(2*a);
double y = Math.sqrt (-d)/(2*a);
System.out.println("r1 = "+x+"+i"+y);
System.out.println("r2 = "+x+"+i"+y);
}
}
}
}

package SHARMA;

//20MT1043
//SHARMA

import java.util.*;
public class fibonacciexamle {
static int fibRecursive(int n)
{
if (n <= 1)
return n;
return fibRecursive(n-1) + fibRecursive(n-2);
}

static int fibNonRecursive(int count)


{
int n1=1,n2=1,n3=1;
if (count ==0)
return n1;
if (count==1)
return n2;

for(int i=2;i<=count;++i)
{
n3=n1+n2;
n1=n2;
n2=n3;
}
return n3;
}

public static void main(String args[])


{
Scanner s = new Scanner(System.in);
System.out.println("Enter the value of N");
int n = s.nextInt();
System.out.println("Fibonacci Series using recursive method");
for (int i=1; i<n; i++)
System.out.print(fibRecursive(i)+"\t");
System.out.println("\nFibonacci Series using Non-recursive method");
for (int i=0; i<n; i++)
System.out.print(fibNonRecursive(i)+"\t");
}

package SHARMA;

//20MT1043
//SHARMA

import java.util.Scanner;
public class primeno {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the limit: ");
int limit = input.nextInt();
System.out.print("Prime numbers up to " + limit + " are: ");
for (int i = 2; i <= limit; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}

package SHARMA;
//20MT1043
//SHARMA
import java.util.Scanner;
public class classmatrix {
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 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("The matrices can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter 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];
}
multiply[c][d] = sum;
sum = 0;
}
}
System.out.println("Product of the matrices:");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}
}
}

package SHARMA;

//20MT1043
//SHARMA

import java.util.Scanner;
public class palind {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
if (isPalindrome(inputString)) {
System.out.println(inputString + " is a palindrome.");
} else {
System.out.println(inputString + " is not a palindrome.");
}
}
public static boolean isPalindrome(String inputString) {
int length = inputString.length();
for (int i = 0; i < length / 2; i++) {
if (inputString.charAt(i) != inputString.charAt(length - i - 1)) {
return false;
}
}
return true;
}
}

package SHARMA;

//20MT1043
//SHARMA

import java.util.Scanner;
public class Exp_07 {
String usn,name,department;
void insertstudent(String num,String nm,String dept)
{
usn=num;
name=nm;
department=dept;
}
void displayStudent()
{
System.out.println("-------------------");
System.out.println("Hall Ticket number:"+usn);
System.out.println("Name:"+name);
System.out.println("Department:"+department);
System.out.println("--------------------");
}
public static void main(String[] args)
{
Exp_07 st[]=new Exp_07[100];
Scanner ip=new Scanner(System.in);
System.out.println("Enter the number of Student:");
int n=ip.nextInt();
for(int i=0;i<n;i++)
st[i]=new Exp_07();
for(int j=0;j<n;j++)
{
System.out.println("Enter the Hall Ticket Numder,Name,
Department:");
String usn=ip.next();
String name=ip.next();
String department=ip.next();
st[j].insertstudent(usn,name,department);
}
for(int m=0;m<n;m++)
{
System.out.println("Student Detail are\n"+m+1);
st[m].displayStudent();
}

}
}

package SHARMA;

//20MT1043
//SHARMA
import java.util.Arrays;
import java.util.Scanner;
public class classsort {
public static void main(String[] args)
{
Scanner sc = new Scanner ( System.in);
int n;
System.out.println("Enter the number of elements:");
n = sc.nextInt();
String names[]= new String[n];
System.out.println("Enter the string:");
Scanner sc1 = new Scanner (System.in);
for(int i=0;i<n;i++) {
names[i]=sc1.nextLine();}
{
for(int i=0;i<n;i++)
{
for(int j =i+1;j<n;j++) {
if(names[i].compareTo(names[j])>0){
String temp = names[i];
names[i] = names[j];
names[j]=temp;
}
}
}
System.out.println(Arrays.toString(names));
}
}
}
package SHARMA;

//20MT1043
//SHARMA
import java.io.*;
public class filecopy {

public static void main(String[] args) {


String sourceFilePath = "source.txt";
String destinationFilePath = "destination.txt";
try {
File sourceFile = new File(sourceFilePath);
File destinationFile = new File(destinationFilePath);
FileInputStream input = new FileInputStream(sourceFile);
FileOutputStream output = new FileOutputStream(destinationFile);

byte[] buffer = new byte[1024];


int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}

input.close();
output.close();

System.out.println("File copied successfully!");


} catch (FileNotFoundException e) {
System.err.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
}
}
}

package SHARMA;

//20MT1043
//SHARMA

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class file {


public static void main(String[] args) {
try {
File file = new File("example.txt");
BufferedReader br = new BufferedReader(new FileReader(file));

String line;
int lineNumber = 1;

while ((line = br.readLine()) != null) {


System.out.println(lineNumber + ": " + line);
lineNumber++;
}

br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

package SHARMA;

//20MT1043
//SHARMA

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class line {


public static void main(String[] args) {
try {
File file = new File("example.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int charCount = 0;
int wordCount = 0;
int lineCount = 0;

String line;
while ((line = br.readLine()) != null) {
lineCount++;
String[] words = line.split("\\s+");
wordCount += words.length;
for (String word : words) {
charCount += word.length();
}
}

System.out.println("Number of characters: " + charCount);


System.out.println("Number of words: " + wordCount);
System.out.println("Number of lines: " + lineCount);

br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package SHARMA;

//20MT1043
//SHARMA

import java.awt.*;
import java.awt.event.*;
public class MouseEventExample extends Frame implements MouseListener {

public MouseEventExample() {
addMouseListener(this);
setTitle("MOUSE EVENTS ");
setSize(800, 600);
setVisible(true);
}

public static void main(String[] args) {


new MouseEventExample();
}

public void mouseClicked(MouseEvent e) {


System.out.println("Mouse clicked at: " + e.getX() + ", " + e.getY());
}

public void mouseEntered(MouseEvent e) {


System.out.println("Mouse entered at: " + e.getX() + ", " + e.getY());
}

public void mouseExited(MouseEvent e) {


System.out.println("Mouse exited at: " + e.getX() + ", " + e.getY());
}

public void mousePressed(MouseEvent e) {


System.out.println("Mouse pressed at: " + e.getX() + ", " + e.getY());
}

public void mouseReleased(MouseEvent e) {


System.out.println("Mouse released at: " + e.getX() + ", " + e.getY());
}
}
package SHARMA;

//20MT1043
//SHARMA

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyEventExample extends Frame {


private Label label;

public KeyEventExample() {
setTitle("Key Event ");
setSize(300, 200);
setLayout(new FlowLayout());

label = new Label("Press a key");


add(label);

addKeyListener((KeyListener) new MyKeyListener());

setVisible(true);
}

public static void main(String[] args) {


new KeyEventExample();
}

private class MyKeyListener extends KeyAdapter {


public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
label.setText("Up arrow pressed");
break;
case KeyEvent.VK_DOWN:
label.setText("Down arrow pressed");
break;
case KeyEvent.VK_LEFT:
label.setText("Left arrow pressed");
break;
case KeyEvent.VK_RIGHT:
label.setText("Right arrow pressed");
break;
default:
label.setText("Key pressed: " + e.getKeyChar());
break;
}
}
}
}

package SHARMA;

//20MT1043
//SHARMA

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;

public class drawing extends JFrame implements MouseListener,MouseMotionListener {


private static final int WIDTH = 800;
private static final int HEIGHT = 600;

private int startX, startY, endX, endY;


private String shape = "rectangle";
public drawing() {
setTitle("Draw Shapes ");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
addMouseMotionListener((MouseMotionListener) this);
setVisible(true);
}

public void paint(Graphics g) {


super.paint(g);
if (shape.equals("line")) {
g.drawLine(startX, startY, endX, endY);
} else if (shape.equals("rectangle")) {
int x = Math.min(startX, endX);
int y = Math.min(startY, endY);
int width = Math.abs(startX - endX);
int height = Math.abs(startY - endY);
g.drawRect(x, y, width, height);
} else if (shape.equals("oval")) {
int x = Math.min(startX, endX);
int y = Math.min(startY, endY);
int width = Math.abs(startX - endX);
int height = Math.abs(startY - endY);
g.drawOval(x, y, width, height);
}
}

public void mouseClicked(MouseEvent e) {


// Not used
}

public void mousePressed(MouseEvent e) {


startX = e.getX();
startY = e.getY();
}

public void mouseReleased(MouseEvent e) {


endX = e.getX();
endY = e.getY();
repaint();
}

public void mouseEntered(MouseEvent e) {


// Not used
}

public void mouseExited(MouseEvent e) {


// Not used
}

public void mouseDragged(MouseEvent e) {


endX = e.getX();
endY = e.getY();
repaint();
}

public void mouseMoved(MouseEvent e) {


// Not used
}

public static void main(String[] args) {


new drawing();
}
}
package SHARMA;

//20MT1043
//SHARMA

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleCalculator {


public static void main(String[] args) {
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class CalculatorFrame extends JFrame {


public CalculatorFrame() {
setTitle("Simple Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends JPanel implements ActionListener {
private JButton display;
private double result;
private String lastOperator;
private boolean start;

public CalculatorPanel() {
setLayout(new BorderLayout());

result = 0;
lastOperator = "=";
start = true;

display = new JButton("0");


display.setEnabled(false);
add(display, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();


buttonPanel.setLayout(new GridLayout(4, 4));

addButton(buttonPanel, "7");
addButton(buttonPanel, "8");
addButton(buttonPanel, "9");
addButton(buttonPanel, "/");

addButton(buttonPanel, "4");
addButton(buttonPanel, "5");
addButton(buttonPanel, "6");
addButton(buttonPanel, "*");

addButton(buttonPanel, "1");
addButton(buttonPanel, "2");
addButton(buttonPanel, "3");
addButton(buttonPanel, "-");

addButton(buttonPanel, "0");
addButton(buttonPanel, ".");
addButton(buttonPanel, "=");
addButton(buttonPanel, "+");

add(buttonPanel, BorderLayout.CENTER);
}

private void addButton(Container container, String text) {


JButton button = new JButton(text);
button.addActionListener(this);
container.add(button);
}

public void actionPerformed(ActionEvent event) {


String input = event.getActionCommand();

if (start) {
if (input.equals("-")) {
display.setText(input);
start = false;
}
else {
lastOperator = input;
}
}
else {
if (input.equals("+") || input.equals("-") || input.equals("*") || input.equals("/")) {
calculate(Double.parseDouble(display.getText()));
lastOperator = input;
start = true;
}
else if (input.equals("=")) {
calculate(Double.parseDouble(display.getText()));
lastOperator = "=";
start = true;
}
else {
display.setText(display.getText() + input);
}
}
}

private void calculate(double x) {


if (lastOperator.equals("+")) {
result += x;
}
else if (lastOperator.equals("-")) {
result -= x;
}
else if (lastOperator.equals("*")) {
result *= x;
}
else if (lastOperator.equals("/")) {
result /= x;
}
else if (lastOperator.equals("=")) {
result = x;
}
display.setText("" + result);
}
}

package SHARMA;

//20MT1043
//SHARMA

import java.awt.*;
import javax.swing.*;
public class Display_15 extends JFrame
{
public Display_15()
{
setTitle("Centered Message");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("message",SwingConstants.CENTER);


Font font = new Font("Jokerman", Font.BOLD, 24);
label.setFont(font);
getContentPane().add(label);

setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] args)
{
new Display_15();
}
}
package SHARMA;

//20MT1043
//SHARMA

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private String operator = "";
private int result = 0;
private boolean start = true;

public Calculator() {
setTitle("Calculator");
setSize(250, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();


panel.setLayout(new GridLayout(4, 4));

display = new JTextField("0");


display.setEditable(false);
panel.add(display);

String[] buttons = {"7", "8", "9", "/",


"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"};
for (int i = 0; i < buttons.length; i++) {
JButton button = new JButton(buttons[i]);
button.addActionListener(this);
panel.add(button);
}

add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();

if (command.charAt(0) >= '0' && command.charAt(0) <= '9') {


if (start) {
display.setText("");
start = false;
}
display.setText(display.getText() + command);
} else if (command.equals("C")) {
display.setText("0");
start = true;
} else if (command.equals("=")) {
if (operator.equals("+")) {
result += Integer.parseInt(display.getText());
} else if (operator.equals("-")) {
result -= Integer.parseInt(display.getText());
} else if (operator.equals("*")) {
result *= Integer.parseInt(display.getText());
} else if (operator.equals("/")) {
result /= Integer.parseInt(display.getText());
} else if (operator.equals("%")) {
result %= Integer.parseInt(display.getText());
} else {
result = Integer.parseInt(display.getText());
}
display.setText("" + result);
operator = "";
start = true;
} else {
operator = command;
result = Integer.parseInt(display.getText());
start = true;
}
}

public static void main(String[] args) {


Calculator calculator = new Calculator();
}
}

You might also like