按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
专注于为中小企业提供成都网站设计、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业丰润免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果
Student.java文件代码如下
package user.java.test;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String no;
private String name;
private double score;
public Student(){}
public Student(String no,String name,double score){
this.no=no;
this.name=name;
this.score=score;
}
public String getNo(){ return no;}
public void setNo(String no){ this.no=no;}
public String getName(){ return name;}
public void setName(String name){ this.name=name;}
public double getScore(){ return score;}
public void setScore(double score){ this.score=score;}
public String toString(){
return "学号:"+no+",姓名:"+name+",成绩:"+score;
}
public static double getAvg(Student[] sArray){
double sum=0,avg;
for(int i=0;isArray.length;i++){
sum=sum+sArray[i].getScore();
}
avg=sum/sArray.length;
return avg;
}
}
Test.java文件代码如下
package user.java.test;
public class Test{
public static void main(String[] args){
Student[] sArray=new Student[5];
sArray[0]=new Student("001","张三",89.5);
sArray[1]=new Student("002","李四",82.5);
sArray[2]=new Student("003","王五",93);
sArray[3]=new Student("004","赵六",73.5);
sArray[4]=new Student("005","孙七",66);
System.out.println("这些学生的平均分:"+Student.getAvg(sArray));
for(int i=0;isArray.length;i++){
System.out.println(sArray[i].toString());
}
}
}
按照题目要求编写的圆,圆锥和测试类的Java程序如下
Test.java文件内容如下
class Circle{
private double r;
private String color;
public Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
public double perimeter(){
return Math.PI*2*r;
}
public double getR(){
return this.r;
}
public void setR(double r){
this.r=r;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圆的半径为"+r+",颜色为"+color;
}
}
class Cone{
private Circle c;
private double h;
private String color;
public Cone(Circle c,double h){
this.c=c;
this.h=h;
}
public double volume(){
return 1.0/3*c.area()*h;
}
public Circle getCircle(){
return this.c;
}
public void setCircle(Circle c){
this.c=c;
}
public double getH(){
return this.h;
}
public void setH(double h){
this.h=h;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圆锥的底面积为"+c.area()+",高为"+h+",颜色为"+color;
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5);
circle1.setColor("红色");
System.out.println(circle1.toString());
System.out.println("圆的面积为"+circle1.area());
System.out.println("圆的周长为"+circle1.perimeter());
Cone circlar1=new Cone(circle1,2.7);
circlar1.setColor("蓝色");
System.out.println(circlar1.toString());
System.out.println("圆锥的体积为"+circlar1.volume());
}
}
①
public class DividedByZeroException extends Exception {
private static final long serialVersionUID = 4420887449631055240L;
public String getMessage() {
return "除数不能为零";
}
}
上面是异常类DividedByZeroException的代码。
②
import java.util.Scanner;
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.print("请输入被除数:");
Scanner scanner1 = new Scanner(System.in);
int input1 = scanner1.nextInt();
System.out.print("请输入除数:");
Scanner scanner2 = new Scanner(System.in);
int input2 = scanner2.nextInt();
System.out.print("两个数相除结果为:");
if (input2 == 0) {
try {
throw new DividedByZeroException();
} catch (DividedByZeroException e) {
e.printStackTrace();
}
} else {
System.out.println(input1 / input2);
}
scanner1.close();
scanner2.close();
}
}
上面是测试类代码和运行结果,麻烦看一下是否能满足要求。
以下是一个简单的HTML网页,包含一个文本框和一个按钮,点击按钮后,将文本框中的文字改变为红色:
!DOCTYPE html
html
head title修改文本框颜色/title
script function changeColor() {
var text = document.getElementById("text").value; document.getElementById("text").style.color = "red";
} /script
/head body
input type="text" id="text"brbr button onclick="changeColor()"确定/button /body/html
这个网页包含两个元素:一个输入框和一个按钮。点击按钮后,调用changeColor()函数,该函数首先获取文本框中的文本,然后将其颜色设置为红色,从而实现了修改文本框颜色的功能。
你可以将上述代码复制到文本编辑器中,保存为HTML文件,然后在浏览器中打开该文件,即可看到实现的效果。