
按照题目要求编写的圆,圆锥和测试类的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文件,然后在浏览器中打开该文件,即可看到实现的效果。
先做两个比较简单的先用:import java.util.Arrays
import java.util.Vector
import java.util.regex.Matcher
import java.util.regex.Pattern
public class Function {
/**
* 设计一个方法,完成字符串的解析。方法定义为:public void myParseString(String inputStr)
* 对于给定的字符串,取得字符串内的各个整数(不考虑小数,),然后将取得的数排序,按从小到大依次打印出来。
* @param args
*/
public static void main(String[] args) {
String s = "aa789bB22cc345dd5.a"
new Function().myParseString(s)
}
public void myParseString(String inputStr){
String mathregix="\\d+"//数字
Vector vector=new Vector()
Pattern fun=Pattern.compile(mathregix)
Matcher match = fun.matcher(inputStr)
while (match.find()) {
vector.add(match.group(0))
}
Object[] obj=vector.toArray()
int[] result=new int[obj.length]
for (int i = 0i <obj.lengthi++) {
result[i]=Integer.parseInt((String) obj[i])
}
Arrays.sort(result)
for (int i = 0i <result.lengthi++) {
System.out.println(result[i])
}
}
}
import java.util.Date
/**
* 有一个学生类(Student),其有四个私有属性,分别为:
* 学号no,字符串型;
* 姓名name,字符串型;
* 年龄age,整型;
* 生日birthday,日期型
* 请:
* 1) 按上面描述设计类;
* 2) 定义对每个属性进行取值,赋值的方法;
* 3) 要求学号不能为空,则学号的长度不能少于5位字符,否则抛异常;
* 4) 年龄范围必须在[1,500]之间,否则抛出异常
*/
public class Student {
private String no
private String name
private int age
private Date birthday
public int getAge() {
return age
}
public void setAge(int age) throws Exception {
if(age<1||age<500)throw new Exception("年龄不合法。")
this.age = age
}
public Date getBirthday() {
return birthday
}
public void setBirthday(Date birthday) {
this.birthday = birthday
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public String getNo() {
return no
}
public void setNo(String no) throws Exception {
if(no==null)throw new Exception("学号不能为空!")
if(no.length()<5)throw new Exception("学号不能少于五位!")
this.no = no
}
}
二、三题
import java.awt.BorderLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import javax.swing.JFileChooser
import javax.swing.JMenu
import javax.swing.JMenuBar
import javax.swing.JMenuItem
import javax.swing.JTextArea
import javax.swing.SwingUtilities
import javax.swing.WindowConstants
/**
* 2. 设计一个GUI界面,要求打界面如下:
*点击文件->打开,打开文件选择器,选择打开给定的java.txt文件,将文件内所有a-z,A-Z之间的字符显示在界面的文本区域内。
* 3. 设置一个GUI界面,界面如下:
* 点击文件->保存,打开文件选择窗体,选择一个保存路径,将本界面文本区域内输入的内容进行过滤,将所有非a-z,A-Z之间的字符保存到C盘下的java.txt文件内。
* Java.txt文件内容如下:
* 我们在2009年第2学期学习Java Programming Design,于2009-6-16考试。
*
*
*
*/
public class FileEditer extends javax.swing.JFrame implements ActionListener {
private JMenuBar menubar
private JMenu file
private JMenuItem open
private JTextArea area
private JMenuItem save
private JFileChooser jfc
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FileEditer inst = new FileEditer()
inst.setLocationRelativeTo(null)
inst.setVisible(true)
}
})
}
public FileEditer() {
super()
initGUI()
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
{
area = new JTextArea()
getContentPane().add(area, BorderLayout.CENTER)
area.setText("文本区")
}
{
menubar = new JMenuBar()
setJMenuBar(menubar)
{
file = new JMenu()
menubar.add(file)
file.setText("文件")
file.setPreferredSize(new java.awt.Dimension(66, 21))
{
open = new JMenuItem()
file.add(open)
open.setText("打开")
open.setBorderPainted(false)
open.setActionCommand("open")
open.addActionListener(this)
}
{
save = new JMenuItem()
file.add(save)
save.setActionCommand("save")
save.setText("保存")
save.addActionListener(this)
}
}
}
{
jfc=new JFileChooser()
}
pack()
setSize(400, 300)
} catch (Exception e) {
e.printStackTrace()
}
}
public void actionPerformed(ActionEvent e) {
if("open".equals(e.getActionCommand())){
int result=jfc.showOpenDialog(this)
if(result==jfc.APPROVE_OPTION){
File file=jfc.getSelectedFile()
try {
byte[] b=new byte[1024]
FileInputStream fis=new FileInputStream(file)
fis.read(b)
fis.close()
String string=new String(b,"GBK")
string=string.replaceAll("[^a-zA-Z]*", "")
area.setText(string.trim())
} catch (FileNotFoundException e1) {
e1.printStackTrace()
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace()
}
}
}else if("save".equals(e.getActionCommand())){
int result=jfc.showSaveDialog(this)
if(result!=jfc.APPROVE_OPTION)return
File file=jfc.getSelectedFile()
try {
if(!file.exists()){
file.createNewFile()
}
String string = area.getText()
string=string.replaceAll("[a-zA-Z]*", "")
byte[] b=string.getBytes()
FileOutputStream fis=new FileOutputStream(file)
fis.write(b)
fis.close()
} catch (FileNotFoundException e1) {
e1.printStackTrace()
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace()
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)