次方和平方根是数学中重要的计算,编程语言中也常用到,此处给出使用Java的类和对象实现计算次方和平方根的方法。代码如下
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ch04_4.pkg14;
class pow
{
public double a;
public double b;
public pow(double sa,double sb)
{
a=sa;
b=sb;
}
public void pows()
{
double s;
s=Math.pow(a,b);
System.out.print("次方为"+s);
}
}
class sqrt
{
public double a;
public sqrt(double sa)
{
a=sa;
}
public void sqrts()
{
double s;
s=Math.sqrt(a);
System.out.print("次方为"+s);
}
}
/**
*
* @author
*/
public class Ch04_414 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
pow z1=new pow(2,3);
sqrt z2=new sqrt(9);
z1.pows();
z2.sqrts();
// TODO code application logic here
}
}