Java
一卷
T1
package Test. A 基础语法. T1 ;
public class FactoriesSum {
public static void main ( String [ ] args) {
int sum = 0 , num= 1 ;
for ( int i= 1 ; i<= 10 ; i++ ) {
num= num* i;
sum= sum+ num;
}
System . out. println ( sum) ;
}
}
T4
package Test. A 基础语法. T4 ;
public class LeapYear {
public static void main ( String [ ] args) {
int count = 0 ;
for ( int year = 1949 ; year <= 2023 ; year++ ) {
if ( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) ) {
System . out. print ( year + " " ) ;
count++ ;
if ( count % 5 == 0 ) {
System . out. println ( ) ;
}
}
}
}
}
T6
package Test. B 面向对象编程. T6 ;
import java. math. * ;
abstract class Shape {
public double getArea ( ) {
return 0 ;
}
public double getPerimeter ( ) {
return 0 ;
}
}
class Circle extends Shape {
private double radius;
public Circle ( double radius) {
this . radius = radius;
}
public double getArea ( ) {
return Math . PI * radius* radius;
}
public double getPerimeter ( ) {
return 2 * Math . PI * radius;
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle ( double length, double width) {
this . length = length;
this . width = width;
}
public double getArea ( ) {
return length* width;
}
public double getPerimeter ( ) {
return 2 * ( length+ width) ;
}
}
public class test {
public static void main ( String [ ] args) {
Circle circle = new Circle ( 5 ) ;
Rectangle rectangle = new Rectangle ( 4 , 6 ) ;
System . out. println ( "圆的面积为:" + circle. getArea ( ) ) ;
System . out. println ( "圆的周长为:" + circle. getPerimeter ( ) ) ;
System . out. println ( "矩形的面积为:" + rectangle. getArea ( ) ) ;
System . out. println ( "矩形的周长为:" + rectangle. getPerimeter ( ) ) ;
}
}
T7
package Test. B 面向对象编程. T7 ;
abstract interface Animal {
void cry ( ) ;
String getAnimalname ( ) ;
}
class Simulator {
public void playground ( Animal animal) {
System . out. println ( animal. getAnimalname ( ) ) ;
animal. cry ( ) ;
}
}
class Dog implements Animal {
public String getAnimalname ( ) {
return "Dog" ;
}
public void cry ( ) {
System . out. println ( "汪汪汪" ) ;
}
}
class Cat implements Animal {
public String getAnimalname ( ) {
return "Cat" ;
}
public void cry ( ) {
System . out. println ( "喵喵喵" ) ;
}
}
public class T7 {
public static void main ( String [ ] args) {
Simulator simulator = new Simulator ( ) ;
simulator. playground ( new Dog ( ) ) ;
simulator. playground ( new Cat ( ) ) ;
}
}
T9
package Test. Java 综合应用. T9 ;
import java. util. Scanner ;
public class ReverseStr {
public static void main ( String [ ] args) {
String s ;
Scanner scanner = new Scanner ( System . in) ;
s = scanner. nextLine ( ) ;
String [ ] words = s. split ( " " ) ;
for ( int i = words. length- 1 ; i>= 0 ; i-- ) {
if ( i != 0 ) {
System . out. print ( words[ i] + " " ) ;
} else {
System . out. print ( words[ i] ) ;
}
}
}
}
二卷
T2
package Test2. A 基础语法. T2 ;
public class SequenceSum {
public static void main ( String [ ] args) {
long sum = 0 ;
long term = 0 ;
for ( int i = 0 ; i < 10 ; i++ ) {
term= term* 10 + 5 ;
sum+= term;
System . out. println ( term) ;
}
System . out. println ( sum) ;
}
}
T3
package Test2. A 基础语法. T3 ;
public class IntegerMin {
public static void main ( String [ ] args) {
int max = 0 ;
int i;
for ( i = 1 ; i < Integer . MAX_VALUE ; i++ ) {
max = max + i;
if ( max > 10000 )
break ;
}
System . out. println ( "满足条件的最小正整数n为:" + i) ;
}
}
T6
package Test2. B 核心语法. T6 ;
abstract class Geometry {
public abstract double getArea ( ) ;
}
class Pillar {
Geometry bottom;
double height;
Pillar ( Geometry bottom, double height) {
this . bottom= bottom; this . height= height;
}
public double getVolume ( ) {
if ( bottom== null ) {
System . out. println ( "没有底,无法计算体积" ) ;
return - 1 ;
}
return bottom. getArea ( ) * height;
}
}
class Circle extends Geometry {
double r;
Circle ( double r) {
this . r= r;
}
public double getArea ( ) {
return ( 3.14 * r* r) ;
}
}
class Rectangle extends Geometry {
double a, b;
Rectangle ( double a, double b) {
this . a= a; this . b= b;
}
public double getArea ( ) {
return a* b;
}
}
class Triangle extends Geometry {
double a, b, c;
public Triangle ( double a, double b, double c) {
this . a = a;
this . b = b;
this . c = c;
}
public double getArea ( ) {
double p= ( a+ b+ c) / 2 ;
return Math . sqrt ( p* ( p- a) * ( p- b) * ( p- c) ) ;
}
}
public class T6 {
public static void main ( String args[ ] ) {
Pillar pillar;
Geometry bottom;
bottom= new Rectangle ( 12 , 10 ) ;
pillar = new Pillar ( bottom, 10 ) ;
System . out. println ( "四棱柱体积为:" + pillar. getVolume ( ) ) ;
bottom= new Circle ( 10 ) ;
pillar = new Pillar ( bottom, 10 ) ;
System . out. println ( "圆柱体积为:" + pillar. getVolume ( ) ) ;
bottom= new Triangle ( 3 , 4 , 5 ) ;
pillar= new Pillar ( bottom, 10 ) ;
System . out. println ( "三棱柱体积为:" + pillar. getVolume ( ) ) ;
}
}
T7
package Test2. B 核心语法. T7 ;
interface ComputerAverage {
public double average ( double a, double b, double c) ;
}
class ArithmeticAverage implements ComputerAverage {
public double average ( double a, double b, double c) {
double aver= 0 ;
aver= ( a+ b+ c) / 3 ;
return aver;
}
}
class GeometryAverage implements ComputerAverage {
public double average ( double a, double b, double c) {
double aver= 0 ;
aver= Math . pow ( ( a* b* c) , 1.0 / 3 ) ;
return aver;
}
}
public class T7 {
public static void main ( String args[ ] ) {
ComputerAverage computer;
double a= 11 , b= 12 , c= 15 ;
computer = new ArithmeticAverage ( ) ;
double result = computer. average ( a, b, c) ;
System . out. printf ( "%5.2f,%5.2f和%5.2f的算术平均值:%5.2f\n" , a, b, c, result) ;
computer = new GeometryAverage ( ) ;
result= computer. average ( a, b, c) ;
System . out. printf ( "%5.2f,%5.2f和%5.2f的几何平均值:%5.2f" , a, b, c, result) ;
}
}
T8
package Test2. C 综合应用. T8 ;
import java. util. InputMismatchException ;
import java. util. Scanner ;
class GetPrice {
public static double givePriceSum ( String cost) {
Scanner scanner = new Scanner ( cost) ;
scanner. useDelimiter ( "[^0123456789.]+" ) ;
double sum= 0 ;
while ( scanner. hasNext ( ) ) {
try { double price = scanner. nextDouble ( ) ;
sum = sum+ price;
} catch ( InputMismatchException exp) {
String t = scanner. next ( ) ;
}
} return sum;
}
public static class PriceSumTest {
public static void main ( String args[ ] ) {
String cost = "欢迎光临美好生活超市,你所买的苹果20.5元,梨20元,铅笔5.8元,香蕉 30.5元,矿泉水20.8元。" ;
double priceSum = GetPrice . givePriceSum ( cost) ;
System . out. printf ( "%s\n你此次花费的金额为:%.2f元\n" , cost, priceSum) ;
}
}
}