编程
编写一个程序,计算箱子的体积,将每个箱子的高度、宽度和长度参数的值传递给构造方法,计算并显示体积。
public class Area {
private double width;
private double height;
private double length;
public Area(double width,double height,double length){
this.height = height;
this.width = width;
this.length = length;
}
public double area(){
return width*height*length;
}
public static void main(String[] args) {
Area a = new Area(20, 10, 10);
System.out.println(a.area());
}
}
编写point类,有两个属性x、y,一个方法distance(Point p1,Point p2),计算两者之间的距离。
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public double distance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
public static void main(String[] args) {
Point point1 = new Point(1, 1);
Point point2 = new Point(4, 5);
System.out.println(point1.distance(point1, point2));
}
}
定义一个接口,声明一个方法area()来计算圆的面积(根据半径的长度),在用一个具体的类实现此接口,在编写一个测试类来使用该接口和子类。
//定义的接口
public interface Inter {
public double area(double R);
}
//具体类实现接口
public class Cricle implements Inter {
double PI = 3.14;
public double area(double R) {
return PI * R * R;
}
}
//测试类使用该接口和子类
public class Text {
public static void main(String[] arg){
Cricle cricle = new Cricle();
System.out.println(cricle.area(10));
}
}
定义一个父类,声明一个方法call_Area()来计算圆的面积(根据半径的长度),在用一个具体的类实现此父类
//抽象类
public abstract class abs_Crice {
double dim;
public abs_Crice(double dim ){
this.dim = dim;
}
public abstract double callArea();
}
//抽象类的实现
public class abs_Text extends abs_Crice{
public abs_Text(double dim){
super(dim);
}
public double callArea(){
return 3.14*dim*dim;
}
public static void main(String[] args) {
abs_Crice abs = new abs_Text(10);
System.out.println(abs.callArea());
}
}
创建一个Customer类,类中的属性有姓名(name),年龄(age),性别(gender),每个属性分别有get/set方法。然后创建两个Customer对象:张立、18、女和王蒙、22、男。把这两个对象存储在ArrayList对象中,然后从ArrayList对象读取出来。
//创建的Customer类
public class Customer {
String name;
int age;
String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
//把两个对象存储在ArrayList对象里面
package com.P160;
import java.util.*;
public class ArrayListDemo {
public static List<Customer> arrayList;
public static void init() {
arrayList = new ArrayList<Customer>(2);
// 创建的两个对象
Customer customer = new Customer();
Customer customer1 = new Customer();
customer.setName("张立");
customer.setAge(18);
customer.setGender("女");
customer1.setName("王猛");
customer1.setAge(22);
customer1.setGender("男");
arrayList.add(customer);
arrayList.add(customer1);
}
public static void toArray() {
Object[] arr = arrayList.toArray();
for (int i = 0; i < arr.length; i++) {
Customer str = (Customer) arr[i];
System.out.println(i + 1 + ":" + str.getName());
}
}
public static void main(String[] arg) {
init();
toArray();
}
}
在D盘中创建文件text.txt,文件中的内容为"hello Java",然后利用流把该文件拷贝到E盘根目录下。
import java.io.*;
public class FileDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("D:/tets.txt");
try {
fw.write("hello java");
fw.close();
FileInputStream fis = new FileInputStream("d:/tets.txt");
FileOutputStream fos = new FileOutputStream("d:/a/tets.txt");
byte[] b = new byte[1024];
while(fis.read(b)!= -1)
{
fos.write(b);
}
fos.close();
fis.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
用eclipse连接mysql数据库
import java.sql.*;
public class MysqlJdbc {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "198876");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from user");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
//别的功能见P200
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}