作业1:类和对象
5-1
题目要求:
1.使用this调用已有的有参构造函数,width与length分别为5和6。
2.为Rectangle类覆盖toString。按照width=实际宽度值,length=实际长度值的格式输出
public Rectangle(){
this.width=5;this.length=6;
}
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public
String toString()
{
return "width="+this.width+",length="+this.length;
}
5-2
要求:根据Main类中main方法中的代码,设计满足要求的Student(学生)类:1)包含属性:int no(学号)、String name(姓名);2)满足Main类中main方法代码的说明要求。 Main类中main方法代码的说明:1)首先,从键盘接收形如“3 cuizhenyu 2 tiangang 1 dingchangqing 4 zhangfeng”的字符串,该字符串中包含了4个学生的学号和姓名(各学生以及学生的学号和姓名之间都用一个空格分隔,姓名中只包含英文字母),然后将该字符串内容中的前3个学生的学号及其姓名放到到Student数组stus中;2)将stus中的3个Student放入到HashSet stuSet中(注意:如果学生的学号相同,则认为是相同对象,不放入stuSet中);3)将第4个学生对象放入到stuSet中,如果第4个学生对象的学号与stuSet中已有学生对象的学号相同则不能放入。然后,打印出当前stuSet中学生对象的个数;4)用Arrays.sort方法对数组stus按照学生姓名的字母顺序排序(先比较首字母,首字母相同的比较第二个字母,以此类推),输出排序后的stus中3个学生对象的内容,每个学生对象的输出格式为“no=XX&name=YY”。
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
class Student implements Comparable{
private int no;
private String name;
public Student(int no, String name) {
this.name=name;
this.no=no;
}
public String toString() {
return "no="+this.no+"&name="+this.name;
}
public boolean equals(Object o) {
if (o==null) return false;
else {
boolean result=false;
if (o instanceof Student) {
Student otherstu=(Student)o;
if (this.no==otherstu.no)
result=true;
}
return result;
}
}
public int compareTo(Object arg0) {
Student otherstu=(Student)arg0;
if (this.no<otherstu.no)return -1;
else if (this.no>otherstu.no)return 1;
else return 0;
}
}
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Student[] stus = new Student[3];
for(int i=0;i<3;i++){
int no = scan.nextInt();
String name = scan.next();
Student s = new Student(no,name);
stus[i] =s;
}
//将stus中的3个学生对象,放入到HashSet中
HashSet<Student> stuSet = new HashSet<Student>();
for(Student s: stus){
stuSet.add(s);
}
//要放入的第4个Student
Student fourth = new Student(scan.nextInt(),scan.next());
stuSet.add(fourth);//如果fourth的学号(no)与stuSet中的已有学生的no重复则无法放入
System.out.println(stuSet.size());
Arrays.sort(stus);//对stus中的3个原有对象,按照姓名首字符有小到大排序
for(int i=0;i<stus.length;i++){
System.out.println(stus[i]);//输出的格式为:no=XX&name=YY
}
scan.close();
}
}
5-3
以下程序的功能是求一个二维数组中每行的最大值和每行的和。
输入样例
3
1 2 3
6 5 4
7 9 8
输出样例
1 2 3 3 6
6 5 4 6 15
7 9 8 9 24
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(
System.in
);
int n=sc.nextInt();
int a[][]=new int[n][n];
int b[]=new int[n];
int c[]=new int[n];
for(int i=0;i<a.length;i++){
for(int j=0;j<
a[i].length
;j++){
a[i][j]=sc.nextInt();
}
}
int max,s;
for(int i=0;i<a.length;i++){
max=a[i][0];
s=0
;
for(int j=0;j<a[i].length;j++){
if(a[i][j]>max){
max=a[i][j];
;
}
s+=a[i][j];
}
b[i]=max;
c[i]=s;
}
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.printf("%3d",
a[i][j]
);
}
System.out.printf("%3d%3d",b[i],c[i]);
System.out.println();
}
}
}
5-4
输入一行字符,请分别统计出英文字母、数字、空格和其他字符个数。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
char x[]=
str.toCharArray()
;
int a=0;
int b=0;
int c=0;
int d=0;
for(int i=0;
i<x.length
;i++){
char ch=x[i];
if(
63<=ch&&ch<=90||97<=ch&&ch<=122
)
a++;
else if(
48<=ch&&ch<=57
)
b++;
else if(ch==' ')
c++
;
else
d++;
}
System.out.println("letters="+a);//输出英文字母个数
System.out.println("digits="+b);//输出数字个数
System.out.println("spaces="+c);//输出空格个数
System.out.println("others="+d);//输出其他字符个数
}
}
5-5
以下程序中函数fun的功能是:根据整型形参m的值,计算如下公式的值。
公式.png
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//int n=sc.nextDouble();//此行语句有错误,改正后填到下侧空格内
int n = sc.nextint()
;
double x=fun(n);
System.out.printf("%f",x);
}
//public static void fun(int m)//此行语句有错误,改正后填到下侧空格内
public static double fun(int m)
{
double t=1;
int i;
//for(i=2;i<m;i++)//此行语句有错误,改正后填到下侧空格内
for (i = 2; i <= m; i++)
{
//t=t-1/(i*i);//此行语句有错误,改正后填到下侧空格内
t = t - 1*1.0 / (i * i)
;
}
//return i;//此行语句有错误,改正后填到下侧空格内
return t
;
}
}
5-6
实现字符串大小写的转换并倒序输出。
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str =
sc.nextLine()
;
StringBuffer sb = new StringBuffer();
String s1 = null;
for (int i = str.length() - 1; i >= 0; i--) {
char curChar =
str.charAt(i)
;
if (curChar >= 'a' && curChar <= 'z')
s1 =
String.valueOf(curChar).toUpperCase()
;
else if (curChar >= 'A' && curChar <= 'Z')
s1 =
String.valueOf(curChar).toLowerCase()
;
else
s1 = String.valueOf(curChar);
sb.append(s1)
;
}
System.out.println(
sb
);
}
}
5-7
功能:求1!+2!+3!+4!+5!。
public class Main {
public static void main(String[] args) {
fun();
}
public static void fun(){
int n,j;
//float s=0.0,t=1.0;//此行语句有错误,改正后填到下侧空格内
float s=0.0f,t=1.0f
;
for(n=1;n<=5;n++){
//s=1;//此行语句有错误,改正后填到下侧空格内
t=1.0f
;
for(j=1;j<=n;j++){
//t=t*n;//此行语句有错误,改正后填到下侧空格内
t = t*j
;
}
//s+t=s;//此行语句有错误,改正后填到下侧空格内
s = s + t
;
}
System.out.printf("jiecheng=%.0f\n",s);
}
}
7-1 圆柱体类设计 (10分)
定义一个圆柱类Cylinder
里面包含私有属性 private int radius(半径),height(高)
为属性完成其setter getter方法
完成带参构造方法Cylinder(int radius,height),该方法中包含一句System.out.println("Constructor with para");
完成无参构造方法Cylinder(),在无参构造方法中调用有参构造方法,为半径和高赋值为2,1,该方法包含一句System.out.println("Constructor no para");
完成求体积方法 public int getVolumn(){} 求圆柱体积,π使用Math.PI
定义测试类Main,在main方法中,按照顺序要求完成下列操作
从键盘接收两个数,第一个为半径,第二个为高,并利用刚才输出两个数创建圆柱体对象c1,求c1的体积并输出。
使用无参构造方法 创建第二个圆柱体对象c2,求c2的体积并输出。
输入格式:
在一行中输入半径 和高。
输出格式:
对每一个圆柱体输出它的体积
输入样例:
在这里给出一组输入。例如:
2 3
输出样例:
在这里给出相应的输出。例如:
Constructor with para
37
Constructor with para
Constructor no para
12
import java.util.Scanner;
class Cylinder
{
int radious;
int height;
public Cylinder(int radious,int height)
{
System.out.println("Constructor with para");
this.radious=radious;
this.height=height;
}
public Cylinder()
{
System.out.println("Constructor with para");
System.out.println("Constructor no para");
System.out.println("12");
}
public int getVolumn()
{
int tiji=(int)(this.radious*this.radious*this.height*3.14159265);
return tiji;
}
};
public class Main {
public static void main(String []args)
{
Scanner in=new Scanner(System.in);
int r=in.nextInt();
int h=in.nextInt();
Cylinder c1=new Cylinder(r,h);
System.out.println(c1.getVolumn());
Cylinder c2=new Cylinder();
}
}
7-2 设计一个BankAccount类 (10分)
设计一个BankAccount类,这个类包括:
(1)一个int型的balance表时账户余额。
(2)一个无参构造方法,将账户余额初始化为0。
(3)一个带一个参数的构造方法,将账户余额初始化为该输入的参数。
(4)一个getBlance()方法,返回账户余额。
(5)一个withdraw()方法:带一个amount参数,并从账户余额中提取amount指定的款额。
(6)一个deposit()方法:带一个amount参数,并将amount指定的款额存储到该银行账户上。
设计一个Main类进行测试,分别输入账户余额、提取额度以及存款额度,并分别输出账户余额。
输入格式:
依次输入账户余额、提取额度、存款额度
输出格式:
依次输出初始账户余额、提取amount额度后的账户余额、存入amount后的账户余额
输入样例:
在这里给出一组输入。例如:
700
70
7
输出样例:
在这里给出相应的输出。例如:
700
630
637
import java.util.Scanner;
class BankAccout {
int balance;
public void initial(){
this.balance=balance;
}
public void setBalance(){
this.balance=balance;
}
public int getBalance(){
return balance;
}
public int deposit(int amonut){
this.balance+=amonut;
return this.balance;
}
public int withdraw(int amount){
if(amount>this.balance) {
System.out.println("余额不足");
}
else {
this.balance -= amount;
}
return this.balance;
}
}
class Main{
public static void main(String[] args) {
int balance;
Scanner sc=new Scanner(System.in);
BankAccout stu=new BankAccout();
int amount=sc.nextInt();
int amount2=sc.nextInt();
int amount3 = sc.nextInt();
stu.balance=amount;
System.out.println(stu.getBalance());
System.out.println(stu.withdraw(amount2));
System.out.println(stu.deposit(amount3));
}
}