import java.util.Scanner;
class Circle
{
private double radius;
public Circle(double radius) {
this.radius = radius;}
public double getRadius() {
return this.radius
;}
public void setRadius(double radius) {
this.radius = radius
;}
public double getArea() {
return
Math.PI*Math.pow(getRadius() , 2)
;}
}
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()
);
}
}
6-1 从抽象类shape类扩展出一个正n边形 (10分)
在一个正n边形(Regular Polygon)中,所有边的边长都相等,且所有角的度数相同(即这个多边形是等边、等角的)。请从下列的抽象类shape类扩展出一个正n边形类RegularPolygon,这个类将正n边形的边数n和边长a作为私有成员,类中包含初始化边数n和边长a的构造方法。 public abstract class shape {// 抽象类 public abstract double getArea();// 求面积 public abstract double getPerimeter(); // 求周长 } 计算正n边形的面积公式为: Area=n×a×a/(tan((180度/n))×4);
类名:RegularPolygon
裁判测试程序样例:
abstract class shape {// 抽象类
/* 抽象方法 求面积 */
public abstract double getArea();
/* 抽象方法 求周长 */
public abstract double getPerimeter();
}
/* 你提交的代码将嵌入到这里 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat d = new DecimalFormat("#.####");// 保留4位小数
int n=input.nextInt();
double side = input.nextDouble();
shape rp = new RegularPolygon(n,side);
System.out.println(d.format(rp.getArea()));
System.out.println(d.format(rp.getPerimeter()));
input.close();
}
}
输入样例:
5
7
输出样例:
84.3034
35
class RegularPolygon extends shape {
private double n;
private double side;
public RegularPolygon(double n, double side) {
this.n = n;
this.side = side;
}
public double getArea() {
return n*side*side/(Math.tan(Math.toRadians(180/n))*4);
}
public double getPerimeter() {
return n*side;
}
}
6-2 定义一个股票类Stock (10分)
定义一个名为Stock的股票类,这个类包括:一个名为symbol的字符串数据域表示股票代码。一个名为name的字符串 数据域表示股票名称。一个名为previousClosingPrice的double数据域,它存储前一日的股票交易价格。一个名为currentPrice数据域,它存储当前的股票交易价格。创建一个有特定代码和名称的股票的构造方法。一个名为getChangePercent()方法返回从previousClosingPrice变化到currentPrice的百分比。
类名为:
Stock
裁判测试程序样例:
import java.util.Scanner;
/* 你提交的代码将被嵌入到这里 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String symbol1=input.next();
String name1=input.next();
Stock stock = new Stock(symbol1, name1);
stock.previousClosingPrice = input.nextDouble();
// Input current price
stock.currentPrice = input.nextDouble();
// Display stock info
System.out.println(stock.name+" Price Changed: " + stock.changePercent() * 100 + "%");
input.close();
}
}
输入样例:
002594
比亚迪
56.98
55.40
输出样例:
比亚迪 Price Changed: -2.77290277290277%
class Stock
{
String symbol;
String name;
double previousClosingPrice;
double currentPrice;
public Stock(String s,String n)
{
symbol=s;name=n;
}
public double changePercent()
{
return -(previousClosingPrice-currentPrice)/previousClosingPrice;
}
}
7-1 找出最大的对象 (10分)
(找出最大的对象)编写一个方法,返回对象数组中最大的对象。方法签名如下: public static Object max(Comparable[] a) public static Object max(Comparable[] a)
import java.util.*; public class Main{ public static Object max(Comparable[] a) { /// 请填写此部分内容 }
public static void main(String[] args){
String[] sArray = new String[5];
Integer[] intArray = new Integer[5];
Scanner input = new Scanner(System.in);
for(int i=0;i
import java.util.*;
public class Main
{
public static Object max(Comparable[] a)
{
Arrays.sort(a);
return a[4];
}
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
Comparable<String>[] a=new Comparable[5];
Comparable<Integer>[] b=new Comparable[5];
for(int i=0;i<5;i++)
a[i]=in.next();
for(int i=0;i<5;i++)
b[i]=in.nextInt();
System.out.println("Max string is "+max(a));
System.out.println("Max integer is "+max(b));
in.close();
}
}
7-2 两个巨大素数(质数)的乘积 (10分)
得到两个巨大素数(质数)的乘积是简单的事,但想从该乘积分解出这两个巨大素数却是国际数学界公认的质因数分解难题。这种单向的数学关系,是不对称加密RSA算法的基本原理。 本题给出两个大素数(128bit位)的乘积和其中一个素数,请你编程求出另一个素数。
输入格式:
44022510695404470886511586569647292146578314354528108825807522926455663589709 (大素数的乘积) 189193782774204832019945226750213439577 (其中一个大素数)
输出格式:
232684764001698545563067004009755869717 (另一个素数)
输入样例:
60883665878129858935918958333091530420746054622405737630613777684610994823161
271963475875372143777333694041058521413
输出样例:
223867067745633357281812540202957589797
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String []args)
{
Scanner cin = new Scanner(System.in);
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
System.out.print(a.divide(b));
cin.close();
}
}
7-3 查找电话号码 (10分)
文件phonebook1.txt中有若干联系人的姓名和电话号码。 高富帅 13312342222 白富美 13412343333 孙悟空 13512345555 唐三藏 13612346666 猪悟能 13712347777 沙悟净 13812348888 请你编写一个简单的通信录程序,当从键盘输入一个姓名时查找到对应的电话号码并输出。如果没找到则显示Not found. 由于目前的自动裁判系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其对应的电话号码。
输入格式:
高富帅 13312342222 白富美 13412343333 孙悟空 13512345555 唐三藏 13612346666 猪悟能 13712347777 沙悟净 13812348888 noname (表示结束) 唐三藏 (需要查找此人的电话号码)
输出格式:
13612346666 (输出对应的电话号码)
输入样例:
白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
noname
白骨精
输出样例:
Not found.
作者: 王博
单位: 西安邮电大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
Map<String,String> map=new HashMap<String, String>();
while(in.hasNext()){
String s=in.next();
if(s.equals("noname")){
break;
}
else{
String string=in.next();
map.put(s, string);
}
}
String tString=in.next();
if(map.containsKey(tString)){
System.out.println(map.get(tString));
}
else {
System.out.println("Not found.");
}
}
}
7-4 Count the letters in a string (10分)
(Count the letters in a string) Write a method that counts the number of letters in a string using the following header: public static int countLetters(String s)
Write a test program that prompts the user to enter a string and displays the number of letters in the string.
input style :
Enter a string
output style:
Displays the number of the letters in the string.
input sample:
qwe&&&,,;#@23qer
output sample:
The number of letters inside the string is: 6
import java.util.*;
public class Main
{
public static void main(String[] args) {
int counter=0;
Scanner in = new Scanner(System.in);
String word= in.next();
char[] charArry=word.toLowerCase().toCharArray();
for(int i=0;i<charArry.length;i++)
{
if(charArry[i]>='a'&&charArry[i]<='z')
{
counter++;
}
}
System.out.printf("The number of letters inside the string is: %d",counter);
}
}
7-5 Remove duplicates (10分)
(Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter n integers to a list ,After sort the distinct intergers and displays the distinct integers separated by exactly one space.
input style :
Input the number n for the length of the array in the first line, and the next line input n integers for the array..
output style:
Displays the distinct integers separated by exactly one space
input sample:
5
32 43 32 22 22
output sample:
32 43 22
package LearningTest;
import java.util.*;
public class Main
{
public static Integer[] threeClear(int[] arr){
return getIntegers(arr);
}
static Integer[] getIntegers(int[] arr) {
List list = new ArrayList();
for(int i=0;i<arr.length;i++)
{
if(!list.contains(arr[i]))
{
list.add(arr[i]);
}
}
return (Integer[]) list.toArray(new Integer[arr.length]);
}
public static Integer[] toIntegerArray(int[] arr){
int n=arr.length;
Integer[] iarr=new Integer[n];
for(int i=0;i<n;i++)
{
arr[i]=new Integer(arr[i]);
}
return iarr;
}
public static void main(String[] args) {
Main arr = new Main();
Scanner in = new Scanner(System.in);
int length = in.nextInt();
int[] num = new int[length];
for (int i = 0; i < num.length; i++) {
num[i] = in.nextInt();
}
Integer[] total = new Integer[length];
total = threeClear(num);
for (int i = 0 ; i<total.length ; i++)
{
if(total[i] != null)
{
System.out.print(total[i] + " ");
}
}
in.close();
}
}
7-6 Combine two lists then sorted it (10分)
(Combine two lists then sorted it.)
Write a method that returns the union of two array lists of integers using the following header:
public static ArrayList union( ArrayList list1, ArrayList list2)
For example, the union of two array lists {2,3,1,5} and {3,4,6} is {2, 3, 1, 5, 3, 4, 6}.
Write a test program that prompts the user to enter two lists, then sorted the union list, finally displays their sorted union. The numbers are separated by exactly one space in the output.
Write a test program that prompts the user to enter the two number m,n for the length of two arrays in the first line, and the next two line input m integer and n intergers for the two array. After sort theunion intergers and displays the sorted union list separated by exactly one space.
input style :
Input the two number m,n for the length of two arrays in the first line, and the next two line input m integer and n intergers .
output style:
Displays the sorted union list separated by exactly one space..
input sample:
3 4
23 44 32
12 43 32 44
output sample:
12 23 32 32 43 44 44
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static int[] addArray(int[] arr1 , int[] arr2)
{
int[] arr0 = new int[arr1.length + arr2.length];
for(int i = 0 ; i<arr1.length ; i++)
{
arr0[i] = arr1[i];
}
for(int j = 0 ; j<arr2.length ; j++)
{
arr0[arr1.length + j] = arr2[j];
}
return arr0;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int length_1 = in.nextInt();
int length_2 = in.nextInt();
int[] arr1 = new int[length_1];
int[] arr2 = new int[length_2];
for(int i = 0 ; i<length_1 ; i++)
{
arr1[i] = in.nextInt();
}
for(int j = 0 ; j<length_2 ; j++)
{
arr2[j] = in.nextInt();
}
int[] arr0 = addArray(arr1 , arr2);
Arrays.sort(arr0);
for(int k : arr0)
{
System.out.print(k + " ");
}
}
}
7-7 Geometry: intersecting point (II) (10分)
(Geometry: intersecting point) Suppose two line segments intersect. The two end-points for the first line segment are (x1, y1) and (x2, y2) and for the second line segment are (x3, y3) and (x4, y4).Write a program that prompts the user to enter these four endpoints and displays the intersecting point. the intersecting point can be found by solving a linear equation. Write the LinearEquation class in Programming to solve this equation. You need to keep the last three decimal places. (你需要保留小数点后三位)
input style :
Enter eight double number
output style:
Displays the intersecting point or The two lines are parallel .
input sample:
2.3 4.5 6.7 -3.4 4 5.6 6.7 9.1
output sample:
The intersecting point is at (2.657,3.859)
1 2 3 4 5 6 7 8
output sample:
The two lines are parallel
import java.util.Scanner;
public class Main {
public static double crossPointX(double line1x1, double line1y1, double line1x2,
double line1y2, double line2x1, double line2y1, double line2x2,
double line2y2) {
double x = (line1x1 * (line1y2 - line1y1) / (line1x2 - line1x1)
- line2x1 * (line2y2 - line2y1) / (line2x2 - line2x1) + line2y1 - line1y1)
/ ((line1y2 - line1y1) / (line1x2 - line1x1) - (line2y2 - line2y1)
/ (line2x2 - line2x1));
return x;
}
public static double crossPointY(double linex1, double liney1, double linex2,
double liney2, double x) {
double y = (liney2 - liney1) * (x - linex1) / (linex2 - linex1) + liney1;
return y;
}
public static void main(String[] args) {
double x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, x4 = 0, y4 = 0, x = 0, y = 0;
Scanner input = new Scanner(System.in);
x1 = input.nextDouble();
y1 = input.nextDouble();
x2 = input.nextDouble();
y2 = input.nextDouble();
x3 = input.nextDouble();
y3 = input.nextDouble();
x4 = input.nextDouble();
y4 = input.nextDouble();
double crossX = crossPointX(x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4);
double crossY = crossPointY(x1 , y1 , x2 , y2 , crossX);
if ((y1 - y2)/(x1 - x2) == (y3 - y4)/(x3 - x4)) {
System.out.println("The two lines are parallel");
} else{
System.out.printf("The intersecting point is at (%.3f,%.3f)",crossX, crossY);
}
input.close();
}
}