uk8
import java.util.Scanner;
class Sphere {
private double radius;
public Sphere() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the radius of the sphere: ");
this.radius = scanner.nextDouble();
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getVolume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
public double getSurfaceArea() {
return 4 * Math.PI * Math.pow(radius, 2);
}
}
public class Main{
public static void main(String[] args){
Sphere sphere = new Sphere();
double volume = sphere.getVolume();
double surfaceArea = sphere.getSurfaceArea();
System.out.println("Volume: " + volume);
System.out.println("Surface area: " + surfaceArea);
}
}
Q2)
package Practicals;
import java.awt.;
import java.awt.event.;
class MyFrame extends Frame
{
TextField t,t1;
Label l,l1;
int x,y;
Panel p;
MyFrame(String title)
{
super(title);
setLayout(new FlowLayout());
p=new Panel();
p.setLayout(new GridLayout(2,2,5,5));
t=new TextField(20);
l= new Label("Co-ordinates of clicking");
l1= new Label("Co-ordinates of movement");
t1=new TextField(20);
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
add(p);
addMouseListener(new MyClick());
addMouseMotionListener(new MyMove());
setSize(500,500);
setVisible(true);
}
class MyClick extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
x=me.getX();
y=me.getY();
t.setText("X="+x+" Y="+y);
}
}
class MyMove extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
x=me.getX();
y=me.getY();
t1.setText("X="+ x +" Y="+y);
}
}
}
public class Ass5_SetA2
{
public static void main(String args[])
{
new MyFrame("Mouse locator");
} }