package cn. itcase. kaoshi ;
public class test1 {
public static void main ( String [ ] args) {
int [ ] [ ] k= new int [ 6 ] [ 5 ] ;
for ( int i= 1 ; i<= 5 ; i++ ) {
for ( int j= 1 ; j<= 4 ; j++ ) {
k[ i] [ j] = i* j;
}
}
System . out. println ( k[ 2 ] [ 3 ] ) ;
}
}
public class test1{
public static void main ( String [ ] args) {
int n= args. length;
System . out. println ( "字符串的个数是" + n) ;
for ( int i= 0 ; i< n; i++ ) {
System . out. println ( "第" + ( i+ 1 ) + "个字符串的长度为" + args[ i] . length ( ) ) ;
}
}
}
import java. util. Scanner ;
public class test1{
public static void main ( String [ ] args) {
Scanner input= new Scanner ( System . in) ;
int n= input. nextInt ( ) ;
for ( int i= 1 ; i<= n; i++ ) {
for ( int j= 1 ; j<= n- i; j++ ) {
System . out. print ( " " ) ;
}
for ( int j= 1 ; j<= 2 * i- 1 ; j++ ) {
System . out. print ( "*" ) ;
}
System . out. println ( ) ;
}
}
}
import java. util. Scanner ;
public class test1{
public static void main ( String [ ] args) {
Scanner input= new Scanner ( System . in) ;
int n= input. nextInt ( ) ;
for ( int i= 1 ; i<= n; i++ ) {
for ( int j= 1 ; j<= n- i; j++ ) {
System . out. print ( " " ) ;
}
for ( int j= 1 ; j<= i; j++ ) {
System . out. print ( "*" ) ;
}
System . out. println ( ) ;
}
}
}
package com. graphic ;
public class Cylinder {
private double r;
private double h;
public Cylinder ( double r, double h) {
this . r= r;
this . h= h;
}
public double volume ( ) {
return Math . PI* r* r* h;
}
}
package com. test ;
import com. graphic. Cylinder ;
public class test {
public static void main ( String [ ] args) {
Cylinder c= new Cylinder ( 5.34 , 2 ) ;
System . out. println ( c. volume ( ) ) ;
}
}
package timu6 ;
public class test {
public static void main ( String [ ] args) {
CircleClass c1= new CircleClass ( 0 , 0 , 8.5 , 9 ) ;
CircleClass c2= new CircleClass ( 2 , 3.5 , 9 , 6 ) ;
System . out. println ( c1. area ( ) ) ;
System . out. println ( c2. area ( ) ) ;
}
}
class CircleClass {
private double x0, y0, x, y;
public CircleClass ( double x0, double y0, double x, double y) {
this . x0= x0;
this . y0= y0;
this . x= x;
this . y= y;
}
public double area ( ) {
return Math . PI* Math . sqrt ( ( x- x0) * ( x- x0) + ( y- y0) * ( y- y0) ) ;
}
}
public class test1{
public static void main ( String [ ] args) {
Undergraduate s1= new Undergraduate ( "张三" , 20 , "本科" , "通信专业" ) ;
Undergraduate s2= new Undergraduate ( "李四" , 21 , "本科" , "电子专业" ) ;
Graduate s3= new Graduate ( "王五" , 25 , "硕士" , "无线通信" ) ;
Graduate s4= new Graduate ( "刘六" , 32 , "博士后" , "光纤通信" ) ;
s1. show ( ) ;
s2. show ( ) ;
s3. show ( ) ;
s4. show ( ) ;
}
}
abstract class Student {
String name, degree;
int age;
public Student ( String name, int age, String degree) {
this . name= name;
this . age= age;
this . degree= degree;
}
public abstract void show ( ) ;
}
class Undergraduate extends Student {
private String specialty;
public Undergraduate ( String name, int age, String degree, String specialty) {
super ( name, age, degree) ;
this . specialty= specialty;
}
public void show ( ) {
System . out. println ( name+ ":" + age+ "," + degree+ "," + specialty) ;
}
}
class Graduate extends Student {
private String direction;
public Graduate ( String name, int age, String degree, String direction) {
super ( name, age, degree) ;
this . direction= direction;
}
public void show ( ) {
System . out. println ( name+ ":" + age+ "," + degree+ "," + direction) ;
}
}
public class test1{
public static void main ( String [ ] args) {
Square s1= new Square ( 2 ) ;
Circle s2= new Circle ( 3 ) ;
System . out. println ( s1. area ( 2 ) ) ;
System . out. println ( s2. area ( 3 ) ) ;
}
}
interface Shape {
double area ( double x) ;
}
class Square implements Shape {
double bc;
public Square ( double bc) {
this . bc= bc;
}
public double area ( double x) {
return x* x;
}
}
class Circle implements Shape {
double r;
public Circle ( double r) {
this . r= r;
}
public double area ( double x) {
return Math . PI* x* x;
}
}
public class test1{
public static void main ( String [ ] args) {
RectangleClass r1= new RectangleClass ( 7.8 , 4.5 ) ;
RectangleClass r2= new RectangleClass ( 10.1 , 2.8 ) ;
r1. rectArea ( ) ;
r1. rectLen ( ) ;
r2. rectArea ( ) ;
r2. rectLen ( ) ;
}
}
interface RectangleInterface {
public void rectArea ( ) ;
public void rectLen ( ) ;
}
class RectangleClass implements RectangleInterface {
double x, y;
public RectangleClass ( double x, double y) {
this . x= x;
this . y= y;
}
public void rectArea ( ) {
System . out. println ( x* y) ;
}
public void rectLen ( ) {
System . out. println ( 2 * ( x+ y) ) ;
}
}
import java. util. * ;
public class test1{
public static void main ( String [ ] args) {
String a= "23 10 -8 0 3 7 108" ;
String [ ] b= a. split ( " " ) ;
int [ ] c= new int [ b. length] ;
for ( int i= 0 ; i< b. length; i++ ) {
c[ i] = Integer . parseInt ( b[ i] ) ;
}
Arrays . sort ( c) ;
String ans= "" ;
ans= ans+ c[ 0 ] ;
for ( int i= 1 ; i< b. length; i++ ) {
ans= ans+ " " + c[ i] ;
}
System . out. println ( ans) ;
}
}
public class test1{
public static void main ( String [ ] args) {
String s1= "nba" ;
String s2= "asfasfnabaasdfnbasdnbasnbasdnbadfasdf" ;
int cnt= 0 ;
for ( int i= 0 ; i< s2. length ( ) - s1. length ( ) ; i++ ) {
if ( s2. substring ( i, i+ s1. length ( ) ) . equals ( s1) ) {
System . out. print ( i+ 1 + " " ) ;
cnt++ ;
}
}
System . out. println ( "\n共出现了" + cnt+ "次" ) ;
}
}
public class test1{
public static void main ( String [ ] args) {
int n= Integer . parseInt ( args[ 0 ] ) ;
if ( n% 2 == 0 ) System . out. println ( n+ "是偶数" ) ;
else System . out. println ( n+ "是奇数" ) ;
}
}
public class test1{
public static void main ( String [ ] args) {
int n= Integer . parseInt ( args[ 0 ] ) ;
int m= Integer . parseInt ( args[ 1 ] ) ;
if ( n> m) System . out. println ( n+ ">" + m) ;
else if ( n< m) System . out. println ( n+ "<" + m) ;
else System . out. println ( n+ "=" + m) ;
}
}
public class test1{
public static void main ( String args[ ] ) {
int [ ] [ ] a= new int [ 11 ] [ 11 ] ;
for ( int i= 1 ; i<= 10 ; i++ ) {
a[ i] [ 1 ] = 1 ;
a[ i] [ i] = 1 ;
}
for ( int i= 3 ; i<= 10 ; i++ ) {
for ( int j= 2 ; j< i; j++ ) {
a[ i] [ j] = a[ i- 1 ] [ j- 1 ] + a[ i- 1 ] [ j] ;
}
}
for ( int i= 1 ; i<= 10 ; i++ ) {
for ( int j= 1 ; j<= i; j++ ) {
if ( j!= 1 ) System . out. print ( " " ) ;
System . out. print ( a[ i] [ j] ) ;
}
System . out. println ( ) ;
}
}
}
import java. util. Scanner ;
public class test1{
public static void main ( String [ ] args) {
int num= ( int ) ( Math . random ( ) * 10 ) ;
Scanner input= new Scanner ( System . in) ;
int n= input. nextInt ( ) ;
while ( num!= n) {
if ( num> n) System . out. println ( "sorry,您猜小了!" ) ;
else if ( num< n) System . out. println ( "sorry,您猜大了!" ) ;
n= input. nextInt ( ) ;
}
System . out. println ( "恭喜您,答对了!" ) ;
}
}
public class test1{
public static void main ( String [ ] args) {
Book b= new Book ( "西游记" , "吴承恩" , 200 ) ;
b. printBook ( ) ;
}
}
class Book {
private String name, author;
int sale;
public Book ( ) { }
public Book ( String name, String author, int sale) {
this . name= name;
this . author= author;
this . sale= sale;
}
public void setBook ( String name, String author, int sale) {
this . name= name;
this . author= author;
this . sale= sale;
}
public void printBook ( ) {
System . out. println ( "书名" + name+ " 作者" + author+ " 月销量" + sale) ;
}
}
public class test1{
public static void main ( String [ ] args) {
Account a= new Account ( "abc" , "123" , 100 ) ;
a. show ( ) ;
a. add ( 100 ) ;
a. show ( ) ;
a. qu ( 150 ) ;
a. show ( ) ;
a. qu ( 100 ) ;
a. show ( ) ;
}
}
class Account {
String name, id;
double money;
public Account ( String name, String id, double money) {
this . name= name;
this . id= id;
this . money= money;
}
public void show ( ) {
System . out. println ( "余额为" + money) ;
}
public void add ( double x) {
money= money+ x;
}
public void qu ( double x) {
if ( money< x) {
System . out. println ( "余额不足,操作失败" ) ;
}
else money= money- x;
}
}
package school. garden ;
import biology. animal. human ;
import biology. plant. flower ;
public class test {
public static void main ( String [ ] args) {
human h= new human ( "张三" , 175 , 55 ) ;
flower f= new flower ( "rose" , "res" , "wonderful" ) ;
h. eat ( ) ;
h. sleep ( ) ;
h. work ( ) ;
f. drink ( ) ;
f. blossom ( ) ;
}
}
package biology. animal ;
public class human {
private String name;
double height, weight;
public human ( String name, double height, double weight) {
this . name= name;
this . height= height;
this . weight= weight;
}
public void eat ( ) {
System . out. println ( "eat" ) ;
}
public void sleep ( ) {
System . out. println ( "sleep" ) ;
}
public void work ( ) {
System . out. println ( "work" ) ;
}
}
package biology. plant ;
public class flower {
private String name, color, smell;
public flower ( String name, String color, String smell) {
this . name= name;
this . color= color;
this . smell= smell;
}
public void drink ( ) {
System . out. println ( "drink" ) ;
}
public void blossom ( ) {
System . out. println ( "blossom" ) ;
}
}
package cn. itcase. kaoshi ;
public class test1{
public static void main ( String [ ] args) {
circleClass c1= new circleClass ( 3.5 ) ;
circleClass c2= new circleClass ( 5.0 ) ;
c1. show ( ) ;
c2. show ( ) ;
}
}
interface circleInterface{
static final double PI= Math . acos ( - 1.0 ) ;
double circleArea ( ) ;
}
class circleClass implements circleInterface{
private double r;
public circleClass ( double r) {
this . r= r;
}
public double circleArea ( ) {
return PI* r* r;
}
public void show ( ) {
System . out. println ( "圆的面积为" + circleArea ( ) ) ;
}
}
package cn. itcase. chapter01 ;
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
String [ ] books= new String [ ] { "Computer" , "Hibernate" , "Java" , "Struts" } ;
String [ ] newbook= new String [ books. length+ 1 ] ;
System . out. println ( "插入前的数组为:Computer Hibernate Java Struts" ) ;
System . out. print ( "请输入新书名称:" ) ;
String book= input. next ( ) ;
int index= 0 ;
for ( int i= 0 ; i< books. length; i++ ) {
newbook[ i] = books[ i] ;
}
for ( int i= 0 ; i< books. length; i++ ) {
if ( books[ i] . compareToIgnoreCase ( book) > 0 ) {
index= i;
break ;
}
}
for ( int i= newbook. length- 1 ; i> index; i-- ) {
newbook[ i] = newbook[ i- 1 ] ;
}
newbook[ index] = book;
System . out. print ( "插入后的数组为:" ) ;
for ( int i= 0 ; i< newbook. length; i++ ) {
if ( i!= 0 ) {
System . out. print ( " " ) ;
}
System . out. print ( newbook[ i] ) ;
}
System . out. println ( "\n" ) ;
}
}
package cn. itcase. chapter01 ;
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
int cnt= 0 ;
for ( int i= 3 ; i<= 10000 ; i++ ) {
int x= i;
while ( x!= 1 ) {
if ( x% 2 == 0 ) {
x= x/ 2 ;
}
else {
x= x* 3 + 1 ;
}
}
cnt++ ;
}
System . out. println ( "3-10000之间的所有正整数(共9998个)中有" + cnt + "个数符合角谷猜想" ) ;
}
}
package cn. itcase. chapter01 ;
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
int [ ] a = new int [ 13 ] ;
for ( int i= 1 ; i<= 3600 ; i++ ) {
int num1= ( int ) ( Math . random ( ) * 6 + 1 ) ;
int num2= ( int ) ( Math . random ( ) * 6 + 1 ) ;
int sum= num1+ num2;
a[ sum] ++ ;
}
for ( int i= 2 ; i<= 12 ; i++ ) {
System . out. println ( "3600次中和为" + i+ "的个数为" + a[ i] ) ;
}
}
}
package cn. itcase. kaoshi ;
public class test1{
public static void main ( String [ ] args) {
complex p1= new complex ( ) ;
complex p2= new complex ( ) ;
p1. setrealPart ( 18 ) ;
p1. setimagPart ( 2 ) ;
p2. setrealPart ( 19 ) ;
p2. setimagPart ( - 13 ) ;
complex resultplus= p1. add ( p2) ;
complex resultminus= p1. minus ( p2) ;
System . out. print ( "两个复数相加的结果为:" ) ;
resultplus. print ( ) ;
System . out. print ( "两个复数相减的结果为:" ) ;
resultminus. print ( ) ;
}
}
class complex{
private int realPart;
private int imagPart;
public int getrealPart ( ) {
return realPart;
}
public void setrealPart ( int realPart) {
this . realPart= realPart;
}
public int getimagPart ( ) {
return imagPart;
}
public void setimagPart ( int imagPart) {
this . imagPart= imagPart;
}
public complex add ( complex c) {
int real= this . realPart+ c. realPart;
int imag= this . imagPart+ c. imagPart;
complex result= new complex ( ) ;
result. setrealPart ( real) ;
result. setimagPart ( imag) ;
return result;
}
public complex minus ( complex c) {
int real= this . realPart- c. realPart;
int imag= this . imagPart- c. imagPart;
complex result= new complex ( ) ;
result. setrealPart ( real) ;
result. setimagPart ( imag) ;
return result;
}
public void print ( ) {
if ( imagPart> 0 ) {
System . out. println ( realPart+ "+" + imagPart+ "i" ) ;
}
else if ( imagPart< 0 ) {
System . out. println ( realPart+ "-" + ( - imagPart) + "i" ) ;
}
else {
System . out. println ( realPart) ;
}
}
}
import java. util. Scanner ;
public class test1{
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
System . out. println ( "请输入长方形左上角坐标" ) ;
double x1= input. nextDouble ( ) ;
double y1= input. nextDouble ( ) ;
System . out. println ( "请输入长方形右下角坐标" ) ;
double x2= input. nextDouble ( ) ;
double y2= input. nextDouble ( ) ;
rectangleClass r1 = new rectangleClass ( x1, y1, x2, y2) ;
r1. area ( ) ;
}
}
class rectangleClass{
private double x1, x2, y1, y2;
public rectangleClass ( double x1, double y1, double x2, double y2) {
this . x1= x1;
this . y1= y1;
this . x2= x2;
this . y2= y2;
}
public void area ( ) {
System . out. println ( "以(" + x1+ "," + y1+ ")为左上角顶点坐标,以(" + x2+ "," + y2+ ")在右下角顶点坐标的长方形的面积为:" + ( x2- x1) * ( y1- y2) ) ;
}
}
package cn. itcase. chapter08 ;
public class Student {
private String name;
private int age;
private String degree;
public Student ( String name, int age, String degree) {
super ( ) ;
this . name = name;
this . age = age;
this . degree = degree;
}
public Student ( ) {
super ( ) ;
}
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 getDegree ( ) {
return degree;
}
public void setDegree ( String degree) {
this . degree = degree;
}
public void show ( ) {
System . out. print ( "姓名:" + this . name+ "\t年龄:" + this . age+ "\t学位:" + this . degree) ;
}
}
package cn. itcase. chapter08 ;
class Undergraduate extends Student {
private String specialty;
public String getSpecialty ( ) {
return specialty;
}
public void setSpecialty ( String specialty) {
this . specialty = specialty;
}
public Undergraduate ( String name, int age, String degree, String specialty) {
super ( name, age, degree) ;
this . specialty = specialty;
}
public void show ( ) {
super . show ( ) ;
System . out. println ( "\t专业:" + this . specialty) ;
}
}
package cn. itcase. chapter08 ;
class Graduate extends Student {
private String direction;
public String getDirection ( ) {
return direction;
}
public void setDirection ( String direction) {
this . direction = direction;
}
public Graduate ( String name, int age, String degree, String direction) {
super ( name, age, degree) ;
this . direction = direction;
}
public void show ( ) {
super . show ( ) ;
System . out. println ( "\t研究方向:" + this . direction) ;
}
}
package cn. itcase. chapter08 ;
public class test {
public static void main ( String [ ] ages) {
Undergraduate stu1= new Undergraduate ( "张三" , 20 , "本科" , "通信" ) ;
Undergraduate stu2= new Undergraduate ( "李四" , 21 , "本科" , "电子" ) ;
Graduate stu3= new Graduate ( "王五" , 25 , "硕士" , "通信" ) ;
Graduate stu4= new Graduate ( "刘六" , 36 , "博士" , "通信" ) ;
stu1. show ( ) ;
stu2. show ( ) ;
stu3. show ( ) ;
stu4. show ( ) ;
}
}
package cn. itcase. chapter09 ;
public class Phone {
char [ ] number= new char [ 12 ] ;
public char [ ] getNumber ( ) {
System . out. print ( "本机号码是:" ) ;
for ( int i= 0 ; i< number. length; i++ ) {
System . out. print ( number[ i] ) ;
}
System . out. println ( ) ;
return number;
}
public void setNumber ( char [ ] number) {
this . number = number;
}
}
package cn. itcase. chapter09 ;
class mobilePhone extends Phone {
public void answer ( ) {
System . out. println ( "正通过移动网络接听电话...." ) ;
}
public void dail ( ) {
System . out. println ( "正通过电信固网拨打电话...." ) ;
}
}
package cn. itcase. chapter09 ;
class fixPhone extends Phone {
public void answer ( ) {
System . out. println ( "正通过电信固网接听电话...." ) ;
}
public void dail ( ) {
System . out. println ( "正通过电信固网拨打电话...." ) ;
}
}
package cn. itcase. chapter09 ;
class cordlessPhone extends fixPhone{
public void move ( ) {
System . out. println ( "正在移动通话...." ) ;
}
}
package cn. itcase. chapter09 ;
public class test {
public static void main ( String [ ] args) {
fixPhone fp= new fixPhone ( ) ;
mobilePhone mp= new mobilePhone ( ) ;
cordlessPhone cp= new cordlessPhone ( ) ;
fp. setNumber ( new char [ ] { '0' , '5' , '7' , '4' , '8' , '8' , '2' , '2' , '2' , '0' , '9' , '6' } ) ;
fp. getNumber ( ) ;
fp. dail ( ) ;
fp. answer ( ) ;
mp. setNumber ( new char [ ] { '1' , '3' , '7' , '8' , '8' , '8' , '8' , '6' , '6' , '6' , '6' } ) ;
mp. getNumber ( ) ;
mp. dail ( ) ;
mp. answer ( ) ;
cp. setNumber ( new char [ ] { '2' , '0' , '9' , '6' } ) ;
cp. getNumber ( ) ;
cp. dail ( ) ;
cp. answer ( ) ;
cp. move ( ) ;
}
}
package cn. itcase. chapter08 ;
public abstract class Student {
private String name;
private int age;
private String degree;
public Student ( String name, int age, String degree) {
super ( ) ;
this . name = name;
this . age = age;
this . degree = degree;
}
public Student ( ) {
super ( ) ;
}
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 getDegree ( ) {
return degree;
}
public void setDegree ( String degree) {
this . degree = degree;
}
public void show1 ( ) {
System . out. print ( "姓名:" + this . name+ "\t年龄:" + this . age+ "\t学位:" + this . degree) ;
}
public abstract void show ( ) ;
}
package cn. itcase. chapter08 ;
class Undergraduate extends Student {
private String specialty;
public String getSpecialty ( ) {
return specialty;
}
public void setSpecialty ( String specialty) {
this . specialty = specialty;
}
public Undergraduate ( String name, int age, String degree, String specialty) {
super ( name, age, degree) ;
this . specialty = specialty;
}
public void show ( ) {
super . show1 ( ) ;
System . out. println ( "\t专业:" + this . specialty) ;
}
}
package cn. itcase. chapter08 ;
class Graduate extends Student {
private String direction;
public String getDirection ( ) {
return direction;
}
public void setDirection ( String direction) {
this . direction = direction;
}
public Graduate ( String name, int age, String degree, String direction) {
super ( name, age, degree) ;
this . direction = direction;
}
public void show ( ) {
super . show1 ( ) ;
System . out. println ( "\t研究方向:" + this . direction) ;
}
}
package cn. itcase. chapter08 ;
public class test {
public static void main ( String [ ] ages) {
Student stu1= new Undergraduate ( "张三" , 20 , "本科" , "计算机科学" ) ;
Student stu2= new Undergraduate ( "李四" , 21 , "本科" , "物联网" ) ;
Student stu3= new Graduate ( "王五" , 25 , "硕士" , "软件工程" ) ;
Student stu4= new Graduate ( "刘六" , 36 , "博士" , "通信工程" ) ;
stu1. show ( ) ;
stu2. show ( ) ;
stu3. show ( ) ;
stu4. show ( ) ;
}
}
package cn. itcase. chapter15 ;
public interface Circlelnterface {
final double PI= 3.14 ;
public double circleArea ( double r) ;
}
package cn. itcase. chapter15 ;
public class CircleClass implements Circlelnterface {
double r;
public CircleClass ( double r) {
super ( ) ;
this . r = r;
}
public double circleArea ( double r) {
return ( double ) ( PI* r* r) ;
}
public void printcircle ( ) {
System . out. println ( "半径为" + r+ "的圆面积为:" + circleArea ( r) ) ;
}
}
package cn. itcase. chapter15 ;
public class test {
public static void main ( String [ ] args) {
CircleClass c1= new CircleClass ( 3.5 ) ;
CircleClass c2= new CircleClass ( 5.0 ) ;
c1. printcircle ( ) ;
c2. printcircle ( ) ;
}
}
/**
2. 设计一个Shape接口和它的两个实现类Square和Circle,要求如下:1)Shape接口中有一个抽象方法area(),方法接收一个double类型的参数,返回一个double类型的结果。2)Square和Circle中实现了Shape接口的area()抽象方法,分别求正方形和圆形的面积并返回。在测试类中创建Square和Circle对象,计算边长为2的正方形面积和半径为3的园面积。
*/
package cn.itcase.chapter16;
public interface Shape {
double area(double l);
}
package cn.itcase.chapter16;
public class Square implements Shape{
public double area(double l){
return l*l;
}
}
package cn.itcase.chapter16;
public class Circle {
public double area(double r){
return Math.PI*r*r;
}
}
package cn.itcase.chapter16;
public class test {
public static void main(String[] args){
Shape square= new Square();
Circle circle= new Circle();
System.out.println("边长为2的正方形的面积为:"+square.area(2));
System.out.println("半径为3的圆的面积为"+circle.area(3));
}
}
/**
2. 在biology包中的animal包中有human类,它具有name,height,weight的属性,还具有eat(),sleep()和work()的行为,在biology包中的plant包中有flower类,它具有name,color,smell的属性,还具有drink()和blossom()的行为.现在在一个school包中的garden包中一个张三的人,他是一个human类的对象,种植的rose是一个flower类对象,编程实现并测试各自的方法.
*/
package human;
public class Human {
String name;
double height,weight;
public Human(String name) {
super();
this.name = name;
}
public void eat(){
System.out.println(name+"正在吃");
}
public void sleep(){
System.out.println(name+"正在睡觉");
}
public void work(){
System.out.println(name+"正在工作");
}
}
package plant;
public class Flower {
String name,color,smell;
public Flower(String name) {
super();
this.name = name;
}
public void drink(){
System.out.println(name+"正在吸收水分");
}
public void blossom(){
System.out.println(name+"正在开花");
}
}
package school;
import human.Human;
import plant.Flower;
public class Garden {
public static void main(String[] args){
Human human=new Human("张三");
Flower flower=new Flower("rose");
human.eat();
human.sleep();
human.work();
flower.drink();
flower.blossom();
}
}
/**
汽车总租金
*/
package cn.itcase.chapter10;
public abstract class MotoVehicle {
private String No;
private String Brand;
public MotoVehicle(){
}
public MotoVehicle(String no, String brand) {
super();
No = no;
Brand = brand;
}
public MotoVehicle(String no) {
super();
No = no;
}
public String getNo() {
return No;
}
public void setNo(String no) {
No = no;
}
public String getBrand() {
return Brand;
}
public void setBrand(String brand) {
Brand = brand;
}
public abstract int calcRent(int day);
}
package cn.itcase.chapter10;
public class Bus extends MotoVehicle{
private int seatcount;
public Bus(){
}
public Bus(String No,String Brand,int seatcount) {
super(No,Brand);
this.seatcount = seatcount;
}
public int getSeatcount() {
return seatcount;
}
public void setSeatcount(int seatcount) {
this.seatcount = seatcount;
}
public int calcRent(int day){
if(seatcount>16){
return 1500*day;
}
else{
return 800*day;
}
}
}
package cn.itcase.chapter10;
public class Car extends MotoVehicle{
private String Brand;
public Car(){
}
public Car(String No,String Brand) {
super(No);
this.Brand=Brand;
}
public String getBrand() {
return Brand;
}
public void setBrand(String brand) {
Brand = brand;
}
public int calcRent(int day){
if(Brand.equals("别克商务舱GL8")){
return 600*day;
}
else if(Brand.equals("宝马550i")){
return 500*day;
}
else if(Brand.equals("别克林荫大道")){
return 300*day;
}
else{
System.out.println("输入错误");
return 0;
}
}
}
package cn.itcase.chapter10;
public class Customer {
private String name;
public Customer(String name) {
super();
this.name = name;
}
public static double CalcTotalRent(MotoVehicle[] motos){
double totalRent = 0;
for(int i=0;i<motos.length;i++){
totalRent += motos[i].calcRent(5);
}
return totalRent;
}
}
package cn.itcase.chapter10;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
MotoVehicle[] motos=new MotoVehicle[4];
motos[0] = new Car("京NY28588","宝马550i");
motos[1] = new Car("京NNN3288","宝马550i");
motos[2] = new Car("京NY28588","别克林荫大道");
motos[3] = new Bus("京NY28589","金龙",34);
Customer cust=new Customer("AAA");
double total=cust.CalcTotalRent(motos);
System.out.println("客户名为AAA的某客户"+"租用以下车辆:");
for(int i=0;i<4;i++){
System.out.println(motos[i].getNo()+ "\t" +motos[i].getBrand());
}
System.out.println("总租金为:"+total);
}
}
/**
调色板
*/
package cn.itcase.chapter0527;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class test1{
public static void main(String[] args){
TiaoSe tiaose=new TiaoSe();
}
}
class TiaoSe extends Frame{
Label l1,l2,l3;
Button b1,b2,b3,b4,b5,b6;
TextField tf1,tf2,tf3;
int count1,count2,count3;
Color c;
private class changeText implements ActionListener,TextListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1) tf1.setText((count1++)+"");
if(e.getSource()==b2) tf2.setText((count2++)+"");
if(e.getSource()==b3) tf3.setText((count3++)+"");
if(e.getSource()==b4) tf1.setText((count1--)+"");
if(e.getSource()==b5) tf2.setText((count2--)+"");
if(e.getSource()==b6) tf3.setText((count3--)+"");
int c1=Integer.parseInt(tf1.getText());
int c2=Integer.parseInt(tf2.getText());
int c3=Integer.parseInt(tf3.getText());
if(c1>=255){c1=0;count1=0;}
if(c2>=255){c2=0;count2=0;}
if(c3>=255){c3=0;count3=0;}
if(c1==-1){c1=0;count1=0;}
if(c2==-1){c2=0;count2=0;}
if(c3==-1){c3=0;count3=0;}
c=new Color(c1,c2,c3);
p1.setBackground(c);
}
public void textValueChanged(TextEvent e) {
c=new Color(Integer.parseInt(tf1.getText()),Integer.parseInt(tf2.getText()),Integer.parseInt(tf3.getText()));
p1.setBackground(c);
}
}
Panel p=new Panel();
Panel p1=new Panel();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
public TiaoSe(){
setSize(500,400);
p.setLayout(gbl);
gbc.fill=GridBagConstraints.BOTH;
this.setLabel();
this.setButton_add();
this.setTextField();
this.setButton_sub();
this.setPanel();
add(p);
setVisible(true);
this.addWindowFocusListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
private void setPanel(){
gbc.weightx=10;
gbc.weighty=10;
gbc.gridx=0;
gbc.gridy=4;
gbc.gridwidth=4;
gbc.insets=new Insets(10,10,10,10);
p1.setBackground(new Color(220,220,220));
gbl.setConstraints(p1, gbc);
p.add(p1);
}
private void setButton_sub(){
b4=new Button("-");
b5=new Button("-");
b6=new Button("-");
b4.setFont(new Font("宋体",Font.PLAIN,20));
b5.setFont(new Font("宋体",Font.PLAIN,20));
b6.setFont(new Font("宋体",Font.PLAIN,20));
gbc.gridx=3;
gbc.gridy=0;
gbl.setConstraints(b4, gbc);
gbc.gridy=1;
gbl.setConstraints(b5, gbc);
gbc.gridy=2;
gbl.setConstraints(b6, gbc);
p.add(b4);
p.add(b5);
p.add(b6);
b4.addActionListener(new changeText());
b5.addActionListener(new changeText());
b6.addActionListener(new changeText());
}
private void setTextField(){
tf1=new TextField(5);
tf1.setText("220");
count1=Integer.parseInt(tf1.getText());
tf2=new TextField(5);
tf2.setText("220");
count2=Integer.parseInt(tf2.getText());
tf3=new TextField(5);
tf3.setText("220");
count3=Integer.parseInt(tf3.getText());
gbc.weightx=1;
gbc.weighty=1;
gbc.gridx=2;
gbc.gridy=0;
gbl.setConstraints(tf1, gbc);
gbc.gridy=1;
gbl.setConstraints(tf2, gbc);
gbc.gridy=2;
gbl.setConstraints(tf3, gbc);
p.add(tf1);
p.add(tf2);
p.add(tf3);
tf1.addTextListener(new changeText());
tf2.addTextListener(new changeText());
tf3.addTextListener(new changeText());
this.setKeyPress(tf1);
this.setKeyPress(tf2);
this.setKeyPress(tf3);
}
private void setKeyPress(TextField tf){
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
char ch = e.getKeyChar();
if(!(ch>='0'&&ch<='9')){
e.consume();
}
}
});
}
private void setButton_add(){
b1=new Button("+");
b2=new Button("+");
b3=new Button("+");
gbc.insets=new Insets(10,80,10,10);
gbc.gridx=1;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=1;
gbl.setConstraints(b1, gbc);
gbc.gridy=1;
gbl.setConstraints(b2, gbc);
gbc.gridy=2;
gbl.setConstraints(b3, gbc);
p.add(b1);
p.add(b2);
p.add(b3);
b1.addActionListener(new changeText());
b2.addActionListener(new changeText());
b3.addActionListener(new changeText());
}
private void setLabel(){
l1=new Label("红色",Label.CENTER);
l2=new Label("绿色",Label.CENTER);
l3=new Label("蓝色",Label.CENTER);
l1.setBackground(Color.RED);
l2.setBackground(Color.GREEN);
l3.setBackground(Color.BLUE);
gbc.gridx=0;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=1;
gbc.insets=new Insets(10,10,10,10);
gbl.setConstraints(l1, gbc);
gbc.gridy=1;
gbl.setConstraints(l2, gbc);
gbc.gridy=2;
gbl.setConstraints(l3, gbc);
p.add(l1);
p.add(l2);
p.add(l3);
}
}
package cn.itcase.chapter0527;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
class Chuangkou extends JFrame implements ActionListener{
JButton b1=new JButton("红色");
JButton b2=new JButton("绿色");
JButton b3=new JButton("蓝色");
int count1=0;
int count2=0;
int count3=0;
Container con = this.getContentPane();
public Chuangkou(){
super("按钮和框架");
this.setSize(320,240);
this.setLocation(220,160);
this.setLayout(null);
b1.setSize(80,40);
b2.setSize(80,40);
b3.setSize(80,40);
b1.setLocation(20, 80);
b2.setLocation(120,80);
b3.setLocation(220,80);
b1.setBackground(Color.red);
b2.setBackground(Color.green);
b3.setBackground(Color.blue);
con.add(b1);
con.add(b2);
con.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1){
count1+=10;
if(count1>255) count1=0;
Color c=new Color(count1,count2,count3);
con.setBackground(c);
}
if(e.getSource()==b2){
count2+=10;
if(count2>255) count2=0;
Color c=new Color(count1,count2,count3);
con.setBackground(c);
}
if(e.getSource()==b3){
count3+=10;
if(count3>255) count3=0;
Color c=new Color(count1,count2,count3);
con.setBackground(c);
}
}
}
public class test2 {
public static void main(String args[]){
Chuangkou ck=new Chuangkou();
ck.setVisible(true);
}
}