实验二 Java程序设计基础及简单类的设计
一、实验目的
1.掌握数组类型的声明和动态内存申请,理解数组的引用模型; 2.掌握String类的应用及对字符串变量的操作;
3.掌握类的声明格式和多种封装措施,理解对象的引用模型; 4.掌握成员变量和方法的概念及构造方法的定义和使用; 5.掌握方法的重载。
二、实验内容
1.给定一个一维整型数组,实现以下功能: (1)求数组元素中的最大值; (2)求数组元素中的最小值;
(3)求某个关键字在数组中的序号,若给定关键字在数组中不存在,返回-1; (4)将数组中所有元素按升序重新排列; (5)判断数组的元素序列是否己排序;
要求每个功能用一个方法实现,并编写main方法进行测试。
package java实验二;
import java.util.Arrays;
public class 实验二{ public static int Max(int[] table)
public static int Min(int[] table)
{
int c=table[0]; }
for(int i=1;i
return c;
if(c>table[i])
c=table[i];
{
int b=table[0]; }
for(int i=1;i
return b;
if(b
b=table[i];
public static int 索引位置(int[] table) {
int index=Arrays.binarySearch(table,8); if(index<=0) return -1; else
return index; }
public static int[] 数组排序(int[] table) {
Arrays.sort(table); return table; }
public static boolean 判断排序(int[] table) { }
public static void main(String[] args) {
// TODO 自动生成的方法存根
int[] a=new int[]{2,5,6,8,0,7,9};
System.out.println(\数组是否已经排序:\+判断排序(a)); System.out.println(\数组元素的最大值为:\+实验二.Max(a));
for(int i=0;i
return true;
if(table[i]
for(int j=0;j
if(table[j]>table[j+1])
return false;
System.out.println(\数组元素的最小值为:\+实验二.Min(a)); System.out.println(\的索引位置是:\+实验二.索引位置(a)); a=实验二.数组排序(a);
System.out.print(\排序后的数组:\ ); for(int i=0;i
System.out.print( a[i]+\); System.out.println();
System.out.println(\数组是否已经排序:\+实验二.判断排序(a)); } }
2.编写一个方法找出两个字符串中所有共同的字符,并在main方法中通过键盘输入两个字符串进行测试。
package java实验二;
import java.util.*; import javax.swing.*; public class 寻相同字符 {
public static void main(String[] args) { }
// TODO 自动生成的方法存根
String A=JOptionPane.showInputDialog(\请输入第一个字符串\String A2=JOptionPane.showInputDialog(\请输入第二个字符串\寻相同字符 B=new 寻相同字符(); B.找相同字符(A,A2);
public void 找相同字符(String s1,String s2){ }
char[] c1 = s1.toCharArray(); char[] c2 = s2.toCharArray();
ArrayList
for(int i=0; i
for(int i=0; i
System.out.print(lis.get(i)+ \ for(int j=0; j
if(lis.get(i) == lis.get(j)&i!=j){ }
lis.remove(lis.get(i));
for(int j=0; j
if(c1[i] == c2[j] ){ }
lis.add(c1[i]);
}
3.声明银行账户类,成员变量包括账号、储户姓名、开户时间、身份证号码、存款余额等账户信息,成员方法包括开户、存款、取款、查询余额、销户等操作。
package java实验二;
import java.text.SimpleDateFormat; import java.util.Date;
public class BankAccount { //创建银行账户类
private String _account; //账号 private String _name; //储户姓名 private String _ID; //身份证号码 private Date _accounTimet ; //开户时间 private double _balance; //存款余额
public BankAccount(String account , String name , String ID , double balance ){ //构造方法 进行初始化 this._account = account; this._name = name; this._ID = ID;
this._accounTimet = new Date(); this._balance = balance; System.out.println(\开户成功\ } //存款
public void deposit(double amount){
_balance +=amount;
System.out.println(\成功存款:\ checkAccount(); //方法调用 } //取款
public void withDrawal(double amount){ if(amount > _balance){
System.out.println(\账户:\卡上余额不足,取款失败!\
+ \操作时间:\操作时间()+\ return; }
this._balance = this._balance - amount; System.out.println(\成功取款:\
checkAccount(); }
public Date 操作时间(){ Date a=new Date(); return a; }
public void checkAccount(){
//显示进行操作后的余额和操作的时间
System.out.println(\账户:\卡上余额为:\ +\操作时间:\操作时间()+\ }
public String getID() { return _ID; }
public void setID(String iD) { //建户人证件号码 _ID = iD; }
public String getAccount() { return _account; }
public void setAccount(String account ) { //新建账号名称 this._account =account ; }
public String getName() { return _name; }
public void setName(String name) { //建户人姓名 this._name = name; }
public String getAccounTimet() {
SimpleDateFormat sdf = new SimpleDateFormat(\
return sdf.format(this._accounTimet); }
public void setAccounTimet(Date accounTimet) { //开户时间 this._accounTimet = accounTimet; }
public double getBalance() {
百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典综合文库实验二 Java程序设计基础及简单类的设计在线全文阅读。
相关推荐: