Slip 8
Q1) Create a class Sphere, to calculate the volume and surface area of sphere. (Hint : Surface area=4*3.14(r*r), Volume=(4/3)3.14(r*r*r))
import java.util.*;
public class sphere_Slip8_1
{
public static void main(String args[])
{
int radius;
//double pie=3.14285714286;
Scanner s=new Scanner(System.in);
System.out.println("Enter the Radious");
radius=s.nextInt();
double area=4*(Math.PI)*radius*radius;
System.out.println("Area of the sphere="+area);
double volume=(4.0/3.0)*Math.PI*(radius*radius*radius);
System.out.println("Volume of the sphere="+volume);
}
}
Q2) Design a screen to handle the Mouse Events such as MOUSE_MOVED and MOUSE_CLICKED and display the position of the Mouse_Click in a TextField.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Slip8_2 implements MouseListener,MouseMotionListener
{
JTextField t1;
String msg="";
Slip8_2()
{
// Create and set up the window.
JFrame frame = new JFrame("MouseListenerExample");
// Display the window.
frame.setLayout(new FlowLayout());
t1=new JTextField (30);
frame.addMouseListener(this);
frame.addMouseMotionListener(this);
frame.add(t1);
frame.setSize(500,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mouseClicked(MouseEvent me)
{
msg = "Mouse clicked at X = "+me.getX()+" Y = "+me.getY();
t1.setText(msg);
}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
// Handle button pressed.
public void mousePressed(MouseEvent me) {}
// Handle button released.
public void mouseReleased(MouseEvent me) {}
// Handle mouse dragged.
public void mouseDragged(MouseEvent me) {}
// Handle mouse moved.
public void mouseMoved(MouseEvent me)
{
msg = "Mouse Moved at X = "+me.getX()+" Y = "+me.getY();
t1.setText(msg);
}
public static void main(String[] args)
{
new Slip8_2 ();
}
}
Comments
Post a Comment