1.编写程序计算100以内的素数并输出。
public class Prime {
public static void main(String[] args) {
for(int i = 2; i <= 100; i ++){
int flag = 0;
for(int j = 2; j < i; j ++)
if(i % j == 0){
flag = 1;
break;
}
if(flag == 0){
System.out.println(i);
}
}
}
}
2.有一个未知数,这个数除以三余二,除以五余三,除以七余二,问这个数是多少?
public class Text {
public static void main(String[] args) {
for(int i = 2; ; i ++){
if(i % 3 == 2)
if(i % 5 == 3)
if(i % 7 == 2){
System.out.println(i);
break;
}
}
}
}
3.编写一个账户类Account,它包括一个名为id的int型账号属性,一个名为balance的double型的账号余额属性,定义一个类型为java.util.Date的属性,dateCreated用于记录账号的创建日期。同时,定义无参的构造函数,一个名为withDraw的方法从账号提取特定数目的金额,一个名为deposit的方法向账号存入特定数目的金额。
import java.util.Date;
public class Account {
private final int id;
private double balance;
private final Date dateCreated;
public Account() {
id = 12345;
balance = 1e+10;
dateCreated = new Date();
System.out.println("账号id:" + id + "\n" + "日期:" + dateCreated);
}
public void withDraw(double number) {
balance -= number;
System.out.println("取出" + number + "\n" + "余额" + balance);
}
public void deposit(double number) {
balance += number;
System.out.println("存入" + number + "\n" + "余额" + balance);
}
public static void main(String[] args) {
Account acc = new Account();
acc.withDraw(1e+9);
acc.deposit(1e+9);
}
}
4.编写股票类Stock,这个类包括:
一个名为code的字符串表示股票的代码;
一个名为name的字符串表示股票的代码;
一个名为previousClosingPrice的double型字段,用于存储前一日的收盘价;
一个名为currentPrice的double型字段,用于记录当前的股票价格;
编写一个具有名字和代码的构造函数;
编写一个方法getChangePercent()的方法计算前一天到当前价格的变化百分比;
public class Stock{
private String code;
private String name;
private double previousClosingPrice;
private double currentPrice;
public Stock(String code, String name){
this.code = code;
this.name = name;
this.previousClosingPrice = 75;
this.currentPrice = 125;
}
public double getChangePresent(){
return java.lang.Math.abs(currentPrice-previousClosingPrice)/currentPrice*100;
}
public static void main(){
Stock stock = new Stock("12138","ABCDE");
System.out.println("变化百分比:"+stock.getChangePresent()+"%");
}
}
5.基于位置的服务是目前地图提供的基础服务,设计位置类Location用来表示地图中的一个点(平面上),并定义二维数组表示地图中上商铺的位置,设计一个函数找出距离给定位置最近的商铺的位置。
public class Location {
private int map[][] = new int[3][3];
private int row, col;
public Location(){
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3; j++)
map[i][j] = 0;
row = -1;
col = -1;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public void setMap(int row, int col){
map[row][col]=1;
this.row = row;
this.col = col;
}
public Location locateNearest(Location Mylocation){
int row = -1, col = -1;
double min = 100;
Location nearest = new Location();
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3;j ++)
if(map[i][j] > 0 && Math.sqrt(Math.pow(Mylocation.getCol()-i,2)+Math.pow(Mylocation.getRow()-j,2)) < min){
row = i;
col = j;
min = Math.sqrt(Math.pow(Mylocation.getCol()-i,2)+Math.pow(Mylocation.getRow()-j,2));
}
nearest.setMap(row, col);
return nearest;
}
public static void main(String[] args) {
Location Shop = new Location();
Shop.setMap(2, 2);
Shop.setMap(0, 2);
Location Mylocation = new Location();
Mylocation.setMap(0 ,0);
System.out.println("最近的商铺的位置:"+Shop.locateNearest(Mylocation).getCol()+","+Shop.locateNearest(Mylocation).getCol());
}
}
6.设计一个类用来求解一元二次方程的根,该类有3个成员变量用于表示系数a、b和c。设计方法computeRoot()用于计算方程的解,该方法需要检查系数的合法性,并根据系数给出方程的解。
import java.lang.Math;
public class Eqution {
private double a,b,c;
public Eqution(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public void computeRoot() {
double judge = b*b-4*a*c;
if(judge>0)
System.out.println("无解!");
else if(judge==0)
System.out.println("x1=x2="+(-b+Math.sqrt(judge))/2*a);
else{
System.out.println("x1="+(-b+Math.sqrt(judge))/2*a);
System.out.println("x2="+(-b-Math.sqrt(judge))/2*a);
}
}
public static void main(String args[]){
Eqution eqution = new Eqution(1,2,1);
eqution.computeRoot();
}
}