第三章
1.数据类型的自动转换App3_1
源代码:
public class App3_1
{
public static void main ( String [ ] args)
{
int a = 155 ;
float b = 21.0f ;
System . out. println ( "a = " + a + ",b = " + b) ;
System . out. println ( "a/b = " + ( a/ b) ) ;
}
}
编译运行:
2.整型与浮点数据类型的转换App3_2
源代码:
public class App3_2
{
public static void main ( String [ ] args)
{
int a = 155 ;
int b = 9 ;
float g, h;
System . out. println ( "a = " + a + ",b = " + b) ;
g = a/ b;
System . out. println ( "a/b = " + g + "\n" ) ;
System . out. println ( "a = " + a + ",b = " + b) ;
h = ( float ) a/ b;
System . out. println ( "a/b = " + h) ;
System . out. println ( "(int)h = " + ( int ) h) ;
}
}
编译运行:
3.从键盘输入数据App3_3和App3_3_1
App3_3源代码:
import java. io. * ;
public class App3_3
{
public static void main ( String [ ] args) throws IOException
{
BufferedReader buf;
String str;
buf = new BufferedReader ( new InputStreamReader ( System . in) ) ;
System . out. print ( "请输入字符串:" ) ;
str = buf. readLine ( ) ;
System . out. println ( "您输入的字符串是:" + str) ;
}
}
App3_3编译运行:
App3_3_1源代码:
import java. io. * ;
public class App3_3_1
{
public static void main ( String [ ] args) throws IOException
{
BufferedReader buf;
char str;
buf = new BufferedReader ( new InputStreamReader ( System . in) ) ;
System . out. print ( "请输入字符串:" ) ;
str = ( char ) buf. read ( ) ;
System . out. println ( "您输入的字符串是:" + str) ;
}
}
App3_3_1编译运行:
4.从键盘输入数字,然后将其转换成数值型数据App3_4
源代码:
import java. io. * ;
public class App3_4
{
public static void main ( String [ ] args) throws IOException
{
float num;
String str;
BufferedReader buf;
buf = new BufferedReader ( new InputStreamReader ( System . in) ) ;
System . out. print ( "请输入一个实数:" ) ;
str = buf. readLine ( ) ;
num = Float . parseFloat ( str) ;
System . out. println ( "您输入的数为:" + num) ;
}
}
编译运行:
5.从键盘输入多个数据App3_5
源程序:
import java. io. * ;
public class App3_5
{
public static void main ( String [ ] args) throws IOException
{
int num1, num2;
String str1, str2;
InputStreamReader in;
in = new InputStreamReader ( System . in) ;
BufferedReader buf;
buf = new BufferedReader ( in) ;
System . out. print ( "请输入一个数:" ) ;
str1 = buf. readLine ( ) ;
num1 = Integer . parseInt ( str1) ;
System . out. print ( "请输入第二个数:" ) ;
str2 = buf. readLine ( ) ;
num2 = Integer . parseInt ( str2) ;
System . out. println ( num1 + "*" + num2 + "=" + ( num1 * num2) ) ;
}
}
编译运行:
6.利用Scanner类从键盘输入多个数据App3_6
源代码:
import java. util. * ;
public class App3_6
{
public static void main ( String [ ] args)
{
int num1;
double num2;
Scanner reader = new Scanner ( System . in) ;
System . out. print ( "请输入第一个数:" ) ;
num1 = reader. nextInt ( ) ;
System . out. print ( "请输入第二个数:" ) ;
num2 = reader. nextDouble ( ) ;
System . out. println ( num1 + "*" + num2 + "=" + ( ( float ) num1 * num2) ) ;
}
}
编译运行:
7.利用Scanner类,使用next()和nextLine()方法接收从键盘输入字符串型数据App3_7
App3_7源程序:
import java. util. * ;
public class App3_7
{
public static void main ( String [ ] args)
{
String s1, s2;
Scanner reader = new Scanner ( System . in) ;
System . out. print ( "请输入第一个数据:" ) ;
s1 = reader. nextLine ( ) ;
System . out. print ( "请输入第二个数据:" ) ;
s2 = reader. next ( ) ;
System . out. println ( "输入的是" + s1 + "和" + s2) ;
}
}
App3_7编译运行:
App3_7_1源程序:
import java. util. * ;
public class App3_7_1
{
public static void main ( String [ ] args)
{
String s1, s2;
Scanner reader = new Scanner ( System . in) ;
System . out. print ( "请输入第一个数据:" ) ;
s1 = reader. next ( ) ;
System . out. print ( "请输入第二个数据:" ) ;
s2 = reader. nextLine ( ) ;
System . out. println ( "输入的是" + s1 + "和" + s2) ;
}
}
App3_7_1编译运行:
8.关系运算符和逻辑运算符的使用App3_8
源代码:
public class App3_8
{
public static void main ( String [ ] args)
{
int a = 25 , b = 7 ;
boolean x = a < b;
System . out. println ( "a < b=" + x) ;
int e = 3 ;
boolean y = a/ e> 5 ;
System . out. println ( "x ^ y=" + ( x ^ y) ) ;
if ( b < 0 & e!= 0 ) System . out. println ( "b/0 = " + b/ 0 ) ;
else System . out. println ( "a%e = " + a % e) ;
int f = 0 ;
if ( f!= 0 && a/ f> 5 ) System . out. println ( "a/f = " + a/ f) ;
else System . out. println ( "f = " + f) ;
}
}
编译运行:
第六章
1.定义一个圆柱体类Cylinder1,并创建相应的对象,然后计算圆柱体的底面积与体积App6_1
源代码:
class Cylinder1
{
double radius;
int height;
double pi = 3.14 ;
void area ( )
{
System . out. println ( "底面积 = " + pi * radius * radius) ;
}
double volume ( )
{
return ( pi * radius * radius) * height;
}
}
public class App6_1
{
public static void main ( String [ ] args)
{
Cylinder1 volu;
volu = new Cylinder1 ( ) ;
volu. radius = 2.8 ;
volu. height = 5 ;
System . out. println ( "底圆半径 = " + volu. radius) ;
System . out. println ( "圆柱的高 = " + volu. height) ;
System . out. println ( "圆柱" ) ;
volu. area ( ) ;
System . out. println ( "圆柱体体积 = " + volu. volume ( ) ) ;
}
}
编译运行:
2.同时创建多个圆柱体类Cylinder2的对象,并修改其中一个对象的成员变量pi的值App6_2
源代码:
class Cylinder2
{
double radius;
int height;
double pi = 3.14 ;
void area ( )
{
System . out. println ( "底面积 = " + pi * radius * radius) ;
}
double volume ( )
{
return ( pi * radius * radius) * height;
}
}
public class App6_2
{
public static void main ( String [ ] args)
{
Cylinder2 volu1, volu2;
volu1 = new Cylinder2 ( ) ;
volu2 = new Cylinder2 ( ) ;
volu1. radius = volu2. radius = 2.5 ;
volu2. pi = 3 ;
System . out. println ( "圆柱1底半径 = " + volu1. radius) ;
System . out. println ( "圆柱2底半径 = " + volu2. radius) ;
System . out. println ( "圆柱1的pi值 = " + volu1. pi) ;
System . out. println ( "圆柱2的pi值 = " + volu2. pi) ;
System . out. println ( "圆柱1" ) ;
volu1. area ( ) ;
System . out. println ( "圆柱2" ) ;
volu2. area ( ) ;
}
}
编译运行:
3.以圆柱体类Cylinder3为例来介绍在类内部调用自己的方法App6_3
源代码:
class Cylinder3
{
double radius;
int height;
double pi = 3.14 ;
double area ( )
{
return pi * radius * radius;
}
double volume ( )
{
return area ( ) * height;
}
}
public class App6_3
{
public static void main ( String [ ] args)
{
Cylinder3 volu;
volu = new Cylinder3 ( ) ;
volu. radius = 2.8 ;
volu. height = 5 ;
System . out. println ( "底圆半径 = " + volu. radius) ;
System . out. println ( "圆柱的高 = " + volu. height) ;
System . out. println ( "圆柱" ) ;
System . out. println ( "底面积 = " + volu. area ( ) ) ;
System . out. println ( "圆柱体体积 = " + volu. volume ( ) ) ;
}
}
编译运行:
4.以圆柱体类Cylinder4为例来介绍用变量调用方法App6_4
源代码:
class Cylinder4
{
double radius;
int height;
double pi;
void setCylinder ( double r, int h, double p)
{
pi = p;
radius = r;
height = h;
}
double area ( )
{
return pi * radius * radius;
}
double volume ( )
{
return area ( ) * height;
}
}
public class App6_4
{
public static void main ( String [ ] args)
{
Cylinder4 volu = new Cylinder4 ( ) ;
volu. setCylinder ( 2.5 , 5 , 3.14 ) ;
System . out. println ( "底圆半径 = " + volu. radius) ;
System . out. println ( "圆柱的高 = " + volu. height) ;
System . out. println ( "圆周率pi = " + volu. pi) ;
System . out. println ( "圆柱" ) ;
System . out. println ( "底面积 = " + volu. area ( ) ) ;
System . out. println ( "圆柱体体积 = " + volu. volume ( ) ) ;
}
}
编译运行:
5.以一维数组为参数的方法调用,求若干数的最小值App6_5
源代码:
public class App6_5
{
public static void main ( String [ ] args)
{
int [ ] a = { 8 , 3 , 7 , 88 , 9 , 23 } ;
LeastNumb minNumber = new LeastNumb ( ) ;
minNumber. least ( a) ;
}
}
class LeastNumb
{
public void least ( int [ ] array)
{
int temp = array[ 0 ] ;
for ( int i = 1 ; i < array. length; i++ )
if ( temp > array[ i] )
temp = array[ i] ;
System . out. println ( "最小的数为:" + temp) ;
}
}
编译运行:
6.将一个矩阵转置后输出App6_6
源代码:
public class App6_6
{
public static void main ( String [ ] args)
{
int [ ] [ ] a = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ;
int [ ] [ ] b = new int [ 3 ] [ 3 ] ;
Trans pose = new Trans ( ) ;
b = pose. transpose ( a) ;
for ( int i = 0 ; i < b. length; i++ )
{
for ( int j = 0 ; j < b[ i] . length; j++ )
System . out. print ( b[ i] [ j] + " " ) ;
System . out. print ( "\n" ) ;
}
}
}
class Trans
{
int temp;
int [ ] [ ] transpose ( int [ ] [ ] array)
{
for ( int i = 0 ; i < array. length; i++ )
for ( int j = i + 1 ; j < array[ i] . length; j++ )
{
temp = array[ i] [ j] ;
array[ i] [ j] = array[ j] [ i] ;
array[ j] [ i] = temp;
}
return array;
}
}
编译运行:
7.定义具有一个固定参数和可变参数的方法,然后分别传入不同个数的参数,并输出App6_7
源代码:
public class App6_7
{
public static void display ( int x, String . . . arg)
{
System . out. print ( x + " " ) ;
for ( int i = 0 ; i < arg. length; i++ )
System . out. print ( arg[ i] + " " ) ;
System . out. print ( "\n" ) ;
}
public static void main ( String [ ] args) {
display ( 5 ) ;
display ( 6 , "a" , "b" ) ;
display ( 7 , "AA" , "BB" , "CC" , "DD" ) ;
}
}
编译运行:
第八章
9.运算符instanceof及getName()、getSuperclass()方法的使用App8_9
源代码:
class Person
{
static int count = 0 ;
protected String name;
protected int age;
public Person ( String n1, int a1)
{
name = n1;
age = a1;
this . count++ ;
}
public String toString ( )
{
return this . name + "," + this . age;
}
public void display ( )
{
System . out. println ( "本类名 = " + this . getClass ( ) . getName ( ) + ";" ) ;
System . out. println ( "父类名 = " + this . getClass ( ) . getSuperclass ( ) . getName ( ) ) ;
System . out. print ( "Person.count = " + this . count + " " ) ;
System . out. print ( "Student.count = " + Student . count + " " ) ;
Object obj = this ;
if ( obj instanceof Student )
System . out. println ( obj. toString ( ) + "是Student类对象。" ) ;
else if ( obj instanceof Person )
System . out. println ( obj. toString ( ) + "是Person类对象。" ) ;
}
}
class Student extends Person
{
static int count = 0 ;
protected String dept;
protected Student ( String n1, int a1, String d1)
{
super ( n1, a1) ;
dept = d1;
this . count++ ;
}
public String toString ( )
{
return super . toString ( ) + "," + dept;
}
public void display ( )
{
super . display ( ) ;
System . out. print ( "super.count = " + super . count) ;
System . out. println ( " ;this.count = " + this . count) ;
}
}
public class App8_9
{
public static void main ( String [ ] args)
{
Person per = new Person ( "王永涛" , 23 ) ;
per. display ( ) ;
Student stu = new Student ( "张小三" , 22 , "计算机系" ) ;
stu. display ( ) ;
}
}
编译运行: