很多都是之前的原题
4-2:继承 封装 多态
4-3:BorderLayout
4-4:非模式
下面的程序从键盘输入一个整数,然后输出该整数的10倍数。
import java.io.*;
public class Main {
public static void main(String[] args) {
BufferedReader in=
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please input a integer:");
try{ int i = Integer.valueOf(in.readLine());
System.out.println("Ten times of the number:"+10*i);
in.close();
}catch(IOException e){
System.err.println(e.toString());
}
}
}
求解圆柱体的体积
import java.util.Scanner;
class Circle
{
private double radius;
public Circle(double radius) {this.radius = radius;}
public double getRadius() {
return radius;}
public void setRadius(double radius) {
this.radius=radius;}
public double getArea() {
return radius*radius*Math.PI;}
}
class Cylinder extends Circle{
double height =100;
public Cylinder(double radius,double height) {
super(radius);
this.height = height ;}
public double getVolumn() {
return getArea() * height;
}
}
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double height = sc.nextDouble();
double radius = sc.nextDouble();
Cylinder obj = new Cylinder(radius, height);
System.out.printf("Cylinder obj Volumn is %.2f", obj.getVolumn());
}
}
有一张足够大的纸,厚0.01 毫米。问将它对折多少次后可以达到珠穆朗玛峰的高度(8848.43米)。
class FoldCount {
public int getCount(double h) {
int n =0 ;
while(h<8848.43) {
n++;
h=h*2;
}
return n;
}
}
public class Main {
public static void main(String[] args) {
FoldCount obj;
obj =new FoldCount();
//单位是毫米换成米
System.out.println("count=" +obj.getCount(0.01e-3)) ;
}
}
}
阅读下列说明和Java代码,并填空。 【说明】现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如下图所示:
import java.util.ArrayList;
import java.util.List;
abstract class AbstractFile {
protected String name;
public void printName() {
System.out.println(name);
}
public abstract boolean addChild(AbstractFile file);
public abstract boolean removeChild(AbstractFile file);
public abstract List<AbstractFile> getChildren();
}
class File extends AbstractFile {
public File(String name) {
this.name = name;
}
public boolean addChild(AbstractFile file) {
return false;
}
public boolean removeChild(AbstractFile file) {
return false;
}
public List<AbstractFile> getChildren() {
return null ;
}
}
class Folder extends AbstractFile {
private List<AbstractFile> childList;
public Folder(String name) {
this.name = name;
this.childList = new ArrayList<AbstractFile>();
}
public boolean addChild(AbstractFile file) {
return childList.add(file);
}
public boolean removeChild(AbstractFile file) {
return childList.remove(file);
}
public List <AbstractFile> getChildren() {
return childList ;
}
}
public class Main {
public static void main(String[] args) {
// 构造一个树形的文件/目录结构
AbstractFile rootFolder = new Folder("root");
AbstractFile compositeFolder = new Folder("composite");
AbstractFile windowsFolder = new Folder("windows");
AbstractFile file = new File("TestComposite.java");
compositeFolder.addChild(file);
rootFolder.addChild(compositeFolder);
rootFolder.addChild(windowsFolder);
// 打印目录文件树
printTree(rootFolder);
}
private static void printTree(AbstractFile ifile) {
ifile.printName();
List<AbstractFile> children = ifile.getChildren();
if (children == null)
return;
for (AbstractFile file : children) {
file.printName() ;
}
}
}
设计一个矩形类Rectangle (10分)
设计一个名为Rectangle的类表示矩形。这个类包括: 两个名为width和height的double型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1. 一个无参构造方法。 一个为width和height指定值的矩形构造方法。 一个名为getArea()的方法返回这个矩形的面积。 一个名为getPerimeter()的方法返回这个矩形的周长。
类名为:
Rectangle
裁判测试程序样例:
import java.util.Scanner;
/* 你的代码将被嵌入到这里 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double w = input.nextDouble();
double h = input.nextDouble();
Rectangle myRectangle = new Rectangle(w, h);
System.out.println(myRectangle.getArea());
System.out.println(myRectangle.getPerimeter());
input.close();
}
}
输入样例:
3.14 2.78
输出样例:
8.7292
11.84
class Rectangle{
double width;
double hight;
public Rectangle(){
this.width=1;
this.hight=1;
}
public Rectangle(double width,double hight){
this.width=width;
this.hight=hight;
}
public double getArea(){
return width*hight;
}
public double getPerimeter( ) {
return 2*(width+hight);
}
}
7-4创建一个直角三角形类实现IShape接口
6-1 从抽象类shape类扩展出一个圆形类Circle
7-1 给定三角形的三边,求解三角形的面积
给定三角形的三边,求解三角形的面积。如果给定的三条边的数值不能为构成三角形,要给出It can not create a triangle.
输入格式:
在一行输入用三个空格分开数字,分别代表三角形的三条边。
输出格式:
分别输出三角形的面积,具体输出个格式见输入输出样例。如果不能构成三角形,输出It can not be created a triangle.
输入样例:
3 4 5
输出样例:
The area is: 6.000.
import java.text.DecimalFormat;
import java.util.Scanner;
/*
海伦公式 + DecimalFormat
*/
public class Main{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
DecimalFormat d= new DecimalFormat("#.###");// 保留3位小数
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
if(a+b>c && a+c>b && b+c>a){
double p=(a+b+c)/2;
double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
System.out.println("The area is: "+d.format(area)+".");
}
else{
System.out.println("It can not be created a triangle.");
}
}
}
7-3 找素数
请编写程序,从键盘输入两个整数m,n,找出等于或大于m的前n个素数。
输入格式:
第一个整数为m,第二个整数为n;中间使用空格隔开。例如: 103 3
输出格式:
从小到大输出找到的等于或大于m的n个素数,每个一行。例如: 103 107 109
输入样例:
9223372036854775839 2
输出样例:
9223372036854775907
9223372036854775931
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input= new Scanner(System.in);
BigInteger m;
int n;
m=input.nextBigInteger();
n=input.nextInt();
int cnt=0;
while (cnt<n){
if (m.isProbablePrime(100)){
System.out.println(m);
cnt++;
}
m=m.nextProbablePrime();
}
input.close();
}
}
7-4 找出一个给定字符串某个字符出现的次数
编写一个类,类中有如下的方法,找出一个在给定字符串的某个字符的出现的次数,方法的声明如下: public static int count(String str, char a) 例如, count(“Welcome”, ‘e’) returns 2. 编写一个测试程序,特使用户输入字符串和一个字符,按照提示输出其出现的次数。 下面是输入和输出的样例,请严格按照输入输出格式进行。
输入格式:
please input the string and the character.
输出格式:
Then output the the number of occurrences of a specified character in the string.
输入样例:
Welcome e
输出样例:
The number of occurrences is 2.
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
int count=0;
char ch=sc.next().charAt(0);
for(int i=0;i<s.length();++i) {
if(s.charAt(i)==ch)
count+=1;
}
System.out.println("The number of occurrences is "+count+".");
sc.close();
}
}
7-6 简单的计算器
编程实现一个简单的计算器,实现两个整数的加、减、乘、除。 注意:输入的数字为整数,可能大于Long.MAX_VALUE (又又又是大数!!!)(即: 9223372036854775807)
输入格式:
输入被除数和除数,除号是“/”:
199818221687230837883277970155607217447/15607605175531087007(除法运算)
输出格式:
12802618943776012921 (输出商)
输入样例:
268757455632088758902114193354244344883-187825044215992922193584201757800947591
输出样例:
80932411416095836708529991596443397292
提示:
一. 当字符串对象str存储的为
“199818221687230837883277970155607217447/15607605175531087007"时,下面的方法可以将str从除号”/“处分割成两个字符串: String[] ob = str.split(”\D", 0); 这时,ob[0]的值为
“199818221687230837883277970155607217447”;
ob[1]的值为"15607605175531087007".
二. 如果要检测字符串中是否包含除号’/’,可以用下面的方法检测: (in1.indexOf(’/’) != -1)为true,表示包含’/’。
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(s.indexOf("+")!=-1) {
String[] str=s.split("\\+",0);
BigInteger bg1=new BigInteger(str[0]);
BigInteger bg2=new BigInteger(str[1]);
System.out.println(bg1.add(bg2));
}
if(s.indexOf("-")!=-1) {
String[] str=s.split("-",0);
BigInteger bg1=new BigInteger(str[0]);
BigInteger bg2=new BigInteger(str[1]);
System.out.println(bg1.subtract(bg2));
}
if(s.indexOf("*")!=-1) {
String[] str=s.split("\\*",0);
BigInteger bg1=new BigInteger(str[0]);
BigInteger bg2=new BigInteger(str[1]);
System.out.println(bg1.multiply(bg2));
}
if(s.indexOf("/")!=-1) {
String[] str=s.split("/",0);
BigInteger bg1=new BigInteger(str[0]);
BigInteger bg2=new BigInteger(str[1]);
System.out.println(bg1.divide(bg2));
}
sc.close();
}
}