有关java编程题目?

有关java编程题目?,第1张

按照题目要求编写的圆,圆锥和测试类的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())

}

}

以下是一个简单的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"><br><br>   <button onclick="changeColor()">确定</button> </body></html>

这个网页包含两个元素:一个输入框和一个按钮。点击按钮后,调用changeColor()函数,该函数首先获取文本框中的文本,然后将其颜色设置为红色,从而实现了修改文本框颜色的功能。

你可以将上述代码复制到文本编辑器中,保存为HTML文件,然后在浏览器中打开该文件,即可看到实现的效果。

public class Person {

private String name//姓名

private String sex//性别

public void sayHello() {

System.out.println("姓名:" + name)

System.out.println("性别:" + sex)

}

public Person() {

}

public Person(String name, String sex) {

this.name = name

this.sex = sex

}

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public String getSex() {

return sex

}

public void setSex(String sex) {

this.sex = sex

}

}

public class Student extends Person {

private String num//学号

private String school//学校

public void sayHello() {

super.sayHello()

System.out.println("学号:" + num)

System.out.println("学校:" + school)

}

public Student(String num, String school) {

this.num = num

this.school = school

}

public Student(String name, String sex, String num, String school) {

super(name, sex)

this.num = num

this.school = school

}

public Student() {

}

public String getNum() {

return num

}

public void setNum(String num) {

this.num = num

}

public String getSchool() {

return school

}

public void setSchool(String school) {

this.school = school

}

}

public class Test {

public static void main(String[] args) {

Student stu1 = new Student()

stu1.setName("张三")

stu1.setSex("男")

stu1.setNum("20211225001")

stu1.setSchool("北京大学")

Student stu2 = new Student("20211225002", "北京大学")

stu2.setName("李四")

stu2.setSex("男")

Student stu3 = new Student("王五", "女", "20211225003", "清华大学")

Person person1 = new Person()

person1.setName("赵六")

person1.setSex("女")

Person person2 = new Person("孙七", "女")

stu1.sayHello()

stu2.sayHello()

stu3.sayHello()

person1.sayHello()

person2.sayHello()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12025441.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存