slip.8

1.
import java.util.Scanner;
class Sphere {
 private double radius;
 
 public Sphere(double radius) {
 this.radius = radius;
 }
 
 public double surfaceArea() {
 return 4 * Math.PI * radius * radius;
}
 
 public double volume() {
 return (4.0 / 3.0) * Math.PI * radius * radius * radius;
 }
 
 public void display() {
 System.out.println("Radius of Sphere: " + radius);
 System.out.println("Surface Area : " + surfaceArea());
 System.out.println("Volume : " + volume());
 }
}
public class SphereTest {
 public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 System.out.print("Enter radius of the sphere: ");
 double r = sc.nextDouble();
 Sphere sphere = new Sphere(r);
 sphere.display();
 sc.close();
 }
}

2.
import java.awt.*;
import java.awt.event.*;
public class MouseEventDemo extends Frame implements MouseListener,
MouseMotionListener {
 TextField tf;
public MouseEventDemo() {
 setLayout(new FlowLayout());
 tf = new TextField(30);
 add(new Label("Mouse Click Position:"));
 add(tf);
 
 addMouseListener(this);
 addMouseMotionListener(this);
 setTitle("Mouse Event Demo");
 setSize(400, 200);
 setVisible(true);
 
 addWindowListener(new WindowAdapter() {
 public void windowClosing(WindowEvent we) {
 System.exit(0);
 }
 });
 }
 
 public void mouseClicked(MouseEvent me) {
 int x = me.getX();
 int y = me.getY();
 tf.setText("Clicked at X: " + x + ", Y: " + y);
 }
 public void mousePressed(MouseEvent me) {}
 public void mouseReleased(MouseEvent me) {}
 public void mouseEntered(MouseEvent me) {}
 public void mouseExited(MouseEvent me) {}
 
 public void mouseMoved(MouseEvent me) {
 setTitle("Mouse Moved at X: " + me.getX() + ", Y: " + me.getY());
 }
 public void mouseDragged(MouseEvent me) {}
 public static void main(String[] args) {
 new MouseEventDemo();
}
}

Comments

Popular posts from this blog

slip 25

slip.1

slip.14