7-1 jmu-Java-03面向对象基础-01-构造方法与toString
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
public class Main {
public static void main ( String [ ] args) {
Scanner s = new Scanner ( System . in) ;
class Person {
private String name;
private int age;
private boolean gender;
private int id;
public void NoParameterConstructor ( ) {
System . out. println ( "This is constructor" ) ;
System . out. println ( name+ "," + age+ "," + gender+ "," + id) ;
}
public void setPerson ( String name, int age, boolean gender, int id) {
this . name = name;
this . age = age;
this . gender = gender;
this . id = id;
}
public void getSystemP ( int i) {
String name = s. next ( ) ;
int age = s. nextInt ( ) ;
Boolean gender = s. nextBoolean ( ) ;
setPerson ( name, age, gender, 0 ) ;
}
public void getOut ( Person person) {
System . out. println ( "Person " + "[name=" + person. name+ ", age=" +
person. age+ ", gender=" + person. gender+ ", id=" + person. id+ "]" ) ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public boolean isGender ( ) {
return gender;
}
public void setGender ( boolean gender) {
this . gender = gender;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
}
int n = s. nextInt ( ) ;
Person person = new Person ( ) ;
List < Person > personlList = new ArrayList < > ( ) ;
int i= 0 ;
for ( ; i< n; i++ ) {
person = new Person ( ) ;
person. getSystemP ( i) ;
personlList. add ( person) ;
}
i = personlList. size ( ) - 1 ;
for ( ; i>= 0 ; i-- ) {
person= personlList. get ( i) ;
person. getOut ( person) ;
}
Scanner ss = new Scanner ( System . in) ;
person = new Person ( ) ;
person. NoParameterConstructor( ) ;
person. getOut ( person) ;
}
}
7-2 jmu-Java-03面向对象基础-02-构造方法与初始化块
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
public class Main {
public static class Person {
private String name;
private int age;
private boolean gender;
private int id;
static int ss= 0 ;
{
this . id = this . ss;
System . out. println ( "This is initialization block, id is " + id) ;
this . ss = this . ss + 1 ;
}
static {
System . out. println ( "This is static initialization block" ) ;
}
public void Person ( ) {
System . out. println ( "This is constructor" ) ;
}
public void NoParameterConstructor ( ) {
System . out. println ( "This is constructor" ) ;
System . out. println ( name+ "," + age+ "," + gender+ "," + id) ;
}
public void Person ( String name, int age, boolean gender) {
this . name = name;
this . age = age;
this . gender = gender;
}
public void getOut ( Person person) {
System . out. println ( "Person " + "[name=" + person. name+ ", age=" +
person. age+ ", gender=" + person. gender+ ", id=" + person. id+ "]" ) ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public boolean isGender ( ) {
return gender;
}
public void setGender ( boolean gender) {
this . gender = gender;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
}
public static void main ( String [ ] args) {
Scanner s = new Scanner ( System . in) ;
int n = s. nextInt ( ) ;
Person person;
List < Person > personlList = new ArrayList < > ( ) ;
int i= 0 ;
for ( ; i< n; i++ ) {
person = new Person ( ) ;
String name = s. next ( ) ;
int age = s. nextInt ( ) ;
Boolean gender = s. nextBoolean ( ) ;
person. Person( name, age, gender) ;
personlList. add ( person) ;
}
i = personlList. size ( ) - 1 ;
for ( ; i>= 0 ; i-- ) {
person= personlList. get ( i) ;
person. getOut ( person) ;
}
person = new Person ( ) ;
person. NoParameterConstructor( ) ;
person. getOut ( person) ;
}
}
7-3 定义类
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner in = new Scanner ( System . in) ;
int a, b, c, d, e;
a = in. nextInt ( ) ;
b = in. nextInt ( ) ;
c = in. nextInt ( ) ;
d = in. nextInt ( ) ;
e = in. nextInt ( ) ;
RR rr = new RR ( ) ;
double dd = rr. fun ( a, b, c, d, e) ;
System . out. printf ( "%.2f" , dd) ;
}
}
class RR{
public double fun ( double a, double b, double c, double d, double e) {
double ss = a + b + c + d + e;
ss = ss / 5 ;
return ss;
}
}
7-4 构造方法
public class Main {
public Main ( ) {
System . out. println ( "构造方法一被调用了" ) ;
}
public Main ( int x) {
this ( ) ;
System . out. println ( "构造方法二被调用了" ) ;
}
public Main ( boolean b) {
this ( 1 ) ;
System . out. println ( "构造方法三被调用了" ) ;
}
public static void main ( String [ ] args) {
new Main ( true ) ;
}
}
7-5 类的定义与对象使用
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
class Student {
private String name;
private int age;
private String id;
private boolean show;
public void setStudent ( String id, String name, int age) {
this . show = true ;
if ( age< 7 || age> 60 ) {
this . show = false ;
}
this . name = name;
this . age = age;
this . id = id;
}
public void getStudent ( Student s) {
if ( s. show)
System . out. println ( s. name+ " " + s. id+ " " + s. age) ;
else
System . out. println ( "bad" ) ;
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner s = new Scanner ( System . in) ;
int n = s. nextInt ( ) ;
List < Student > StudentlList = new ArrayList < > ( ) ;
Student student;
int i = 0 ;
for ( ; i< n; i++ ) {
student = new Student ( ) ;
student. setStudent ( s. next ( ) , s. next ( ) , s. nextInt ( ) ) ;
StudentlList . add ( student) ;
}
i= 0 ;
for ( ; i< StudentlList . size ( ) ; i++ ) {
Student s1 = StudentlList . get ( i) ;
s1. getStudent ( s1) ;
}
}
}
7-6 定义类与创建对象
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
class Student {
private String name;
private int age;
private String id;
private boolean show;
public void setStudent ( String name, int age) {
this . show = true ;
if ( age< 7 || age> 60 ) {
this . show = false ;
}
this . name = name;
this . age = age;
}
public void getStudent ( Student s) {
if ( s. show)
System . out. println ( "this person is " + s. name+ ",her age is " + s. age) ;
else
System . out. println ( "bad" ) ;
}
}
public class Main {
public static void main ( String [ ] args) {
List < Student > StudentlList = new ArrayList < > ( ) ;
Student student;
student = new Student ( ) ;
student. setStudent ( "lili" , 19 ) ;
StudentlList . add ( student) ;
student = new Student ( ) ;
student. setStudent ( "lucy" , 20 ) ;
StudentlList . add ( student) ;
int i= 0 ;
for ( ; i< StudentlList . size ( ) ; i++ ) {
Student s1 = StudentlList . get ( i) ;
s1. getStudent ( s1) ;
}
}
}
7-7 学生类设计
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
class Student {
private String name;
private int age;
private String id;
private boolean show;
public void setStudent ( String name, int age) {
this . show = true ;
if ( age< 7 || age> 60 ) {
this . show = false ;
}
this . name = name;
this . age = age;
}
public void getStudent ( Student s) {
if ( s. show)
System . out. println ( "this person is " + s. name+ ",her age is " + s. age) ;
else
System . out. println ( "bad" ) ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
if ( age<= 6 ) this . age= 7 ;
else this . age = age;
}
public String getId ( ) {
return id;
}
public void setId ( String id) {
this . id = id;
}
public boolean isShow ( ) {
return show;
}
public void setShow ( boolean show) {
this . show = show;
}
public void wucan ( ) {
name = "无名" ;
age = 7 ;
System . out. println ( "无参构造方法" ) ;
}
public void display ( ) {
System . out. println ( "name:" + name+ ",age:" + age) ;
}
}
public class Main {
public static void main ( String [ ] args) {
List < Student > StudentlList = new ArrayList < > ( ) ;
Student student;
student = new Student ( ) ;
student. wucan ( ) ;
student. display ( ) ;
Scanner ss = new Scanner ( System . in) ;
student. setName ( ss. next ( ) ) ;
student. setAge ( ss. nextInt ( ) ) ;
student. display ( ) ;
}
}
7-8 学生类-构造函数
import java. util. Scanner ;
class Main {
private String name;
private String sex;
private int age;
public Main ( String name, String sex, int age) {
this . name = name;
this . sex = sex;
this . age = age;
}
@Override
public String toString ( ) {
return "Student [name='" + name + "', sex='" + sex + "', age=" + age + "]" ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getSex ( ) {
return sex;
}
public void setSex ( String sex) {
this . sex = sex;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
String name = scanner. next ( ) ;
int age = scanner. nextInt ( ) ;
String sex = scanner. next ( ) ;
Main student = new Main ( name, sex, age) ;
System . out. println ( student) ;
scanner. close ( ) ;
}
}
7-9 教师类
import java. util. Objects ;
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
class Teacher {
private int no;
String name;
int age;
String seminary;
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
Teacher teacher = ( Teacher ) o;
return no == teacher. no;
}
@Override
public String toString ( ) {
return
"no: " + no +
", name:" + name +
", age: " + age +
", seminary: " + seminary;
}
public void setTeacher ( int no, String name, int age, String seminary) {
this . no = no;
this . name = name;
this . age = age;
this . seminary = seminary;
}
public int getNo ( ) {
return no;
}
public void setNo ( int no) {
this . no = no;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public String getSeminary ( ) {
return seminary;
}
public void setSeminary ( String seminary) {
this . seminary = seminary;
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
List < Teacher > teacherList = new ArrayList < > ( ) ;
Teacher teacher;
for ( int i= 0 ; i< 2 ; i++ ) {
teacher = new Teacher ( ) ;
teacher. setNo ( sc. nextInt ( ) ) ;
teacher. setName ( sc. next ( ) ) ;
teacher. setAge ( sc. nextInt ( ) ) ;
teacher. setSeminary ( sc. next ( ) ) ;
teacherList. add ( teacher) ;
System . out. println ( teacher. toString ( ) ) ;
}
System . out. println ( teacherList. get ( 0 ) . equals ( teacherList. get ( 1 ) ) ) ;
}
}
7-10 统计学生年龄异常的人数。
import java. util. Objects ;
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
class Student {
String name;
int age;
Boolean show;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public Boolean setAge ( int age) {
if ( age> 0 ) this . show = true ;
else this . show = false ;
this . age = age;
return this . show;
}
public void getFlasN ( ) {
if ( ! show) System . out. println ( name) ;
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
List < Student > studentList = new ArrayList < > ( ) ;
Student student;
int num= 0 ;
for ( int i= 0 ; i< 5 ; i++ ) {
student = new Student ( ) ;
student. setName ( sc. next ( ) ) ;
if ( student. setAge ( sc. nextInt ( ) ) ) num+= 1 ;
studentList. add ( student) ;
}
if ( num== 5 ) {
System . out. println ( "right" ) ;
} else if ( num== 0 ) {
System . out. println ( "all wrong" ) ;
} else {
System . out. println ( 5 - num) ;
for ( Student s: studentList) {
s. getFlasN ( ) ;
}
}
}
}
7-11 sdut-oop-4-求圆的面积(类与对象)
import java. util. Objects ;
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
import java. lang. Math ;
class Circle {
private int radius;
public void wucan ( ) {
radius = 2 ;
System . out. println ( "This is a constructor with no para." ) ;
}
public void youcan ( int i) {
if ( i<= 0 ) i= 2 ;
this . radius = i;
System . out. println ( "This is a constructor with para." ) ;
}
public int getRadius ( ) {
return radius;
}
public void setRadius ( int radius) {
if ( radius<= 0 ) radius= 2 ;
this . radius = radius;
}
public void getArea ( ) {
double mj;
mj = radius* radius* Math . PI;
System . out. printf ( "%.2f" , mj) ;
}
@Override
public String toString ( ) {
return "Circle [" +
"radius=" + radius +
']' ;
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
List < Circle > circleList = new ArrayList < > ( ) ;
Circle circle;
circle = new Circle ( ) ;
circle. wucan ( ) ;
System . out. println ( circle. toString ( ) ) ;
circle. getArea ( ) ;
System . out. println ( "" ) ;
circle. wucan ( ) ;
System . out. println ( circle. toString ( ) ) ;
circle. getArea ( ) ;
System . out. println ( "" ) ;
circle. setRadius ( sc. nextInt ( ) ) ;
System . out. println ( circle. toString ( ) ) ;
circle. getArea ( ) ;
System . out. println ( "" ) ;
circle. youcan ( sc. nextInt ( ) ) ;
System . out. println ( circle. toString ( ) ) ;
circle. getArea ( ) ;
}
}
7-12 设计一个矩形类Rectangle
import java. util. Objects ;
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
import java. lang. Math ;
class Rectangle {
private double width;
private double length;
public void setStart ( ) {
this . width = 0 ;
this . length = 0 ;
}
public void setXY ( double a, double b) {
this . width = a;
this . length = b;
}
public double getArea ( ) {
return width* length;
}
public double getPerimeter ( ) {
return 2 * ( width+ length) ;
}
public double getWidth ( ) {
return width;
}
public void setWidth ( double width) {
this . width = width;
}
public double getLength ( ) {
return length;
}
public void setLength ( double length) {
this . length = length;
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
Rectangle s1 = new Rectangle ( ) ;
s1. setWidth ( sc. nextDouble ( ) ) ;
s1. setLength ( sc. nextDouble ( ) ) ;
System . out. println ( "面积为" + s1. getArea ( ) ) ;
System . out. print ( "周长为" + s1. getPerimeter ( ) ) ;
}
}
7-13 学生选课信息管理
import java. util. Objects ;
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
import java. lang. Math ;
class Student {
private String stuID;
private String stuName;
static int stuNum = 0 ;
Student ( ) {
System . out. println ( "学生类无参构造方法" ) ;
stuNum++ ;
}
Student ( String stuID, String stuName) {
System . out. println ( "学生类有参构造方法" ) ;
this . stuID= stuID;
this . stuName= stuName;
stuNum++ ;
}
public String getStuID ( ) {
return stuID;
}
public void setStuID ( String stuID) {
this . stuID = stuID;
}
public String getStuName ( ) {
return stuName;
}
public void setStuName ( String stuName) {
this . stuName = stuName;
}
}
class Course {
private String cID;
private String cName;
static int cNum;
Course ( ) {
System . out. println ( "课程类无参构造方法" ) ;
cNum++ ;
}
Course ( String cID, String cName) {
System . out. println ( "课程类有参构造方法" ) ;
this . cID= cID;
this . cName= cName;
cNum++ ;
}
public String getcID ( ) {
return cID;
}
public void setcID ( String cID) {
this . cID = cID;
}
public String getcName ( ) {
return cName;
}
public void setcName ( String cName) {
this . cName = cName;
}
}
class Schedule {
private List < Student > stuList = new ArrayList < > ( ) ;
private List < Course > cList= new ArrayList < > ( ) ;
static int schNum = 0 ;
public void addCourse ( Student stu, Course course) {
stuList. add ( stu) ;
cList. add ( course) ;
schNum++ ;
}
public void displayCourse ( ) {
System . out. print ( "学生选课情况如下:" ) ;
for ( int i = 0 ; i < stuList. size ( ) ; i++ ) {
System . out. println ( ) ;
System . out. printf ( "%s\t%s\t%s\t%s" ,
stuList. get ( i) . getStuID ( ) ,
stuList. get ( i) . getStuName ( ) ,
cList. get ( i) . getcID ( ) ,
cList. get ( i) . getcName ( ) ) ;
}
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
int type = sc. nextInt ( ) ;
switch ( type) {
case 1 : test1 ( sc) ; break ;
case 2 : test2 ( sc) ; break ;
case 3 : test3 ( sc) ; break ;
case 4 : test4 ( sc) ;
}
}
public static void test1 ( Scanner sc) {
Student student1 = new Student ( ) ;
student1. setStuID ( sc. next ( ) ) ;
student1. setStuName ( sc. next ( ) ) ;
Student student2 = new Student ( ) ;
student2. setStuID ( sc. next ( ) ) ;
student2. setStuName ( sc. next ( ) ) ;
System . out. print ( "学生总数为:" + Student . stuNum) ;
}
public static void test2 ( Scanner sc) {
Course course1 = new Course ( ) ;
course1. setcID ( sc. next ( ) ) ;
course1. setcName ( sc. next ( ) ) ;
Course course2 = new Course ( ) ;
course2. setcID ( sc. next ( ) ) ;
course2. setcName ( sc. next ( ) ) ;
Course course3 = new Course ( ) ;
course3. setcID ( sc. next ( ) ) ;
course3. setcName ( sc. next ( ) ) ;
System . out. print ( "课程总数为:" + Course . cNum) ;
}
public static void test3 ( Scanner sc) {
Student student1 = new Student ( sc. next ( ) , sc. next ( ) ) ;
Course course1 = new Course ( ) ;
course1. setcID ( sc. next ( ) ) ;
course1. setcName ( sc. next ( ) ) ;
Course course2 = new Course ( ) ;
course2. setcID ( sc. next ( ) ) ;
course2. setcName ( sc. next ( ) ) ;
Schedule schedule = new Schedule ( ) ;
schedule. addCourse ( student1, course1) ;
schedule. addCourse ( student1, course2) ;
System . out. println ( "学生选课的总数为:" + Schedule . schNum) ;
schedule. displayCourse ( ) ;
}
public static void test4 ( Scanner sc) {
Student student1 = new Student ( sc. next ( ) , sc. next ( ) ) ;
Student student2 = new Student ( sc. next ( ) , sc. next ( ) ) ;
Student student3 = new Student ( sc. next ( ) , sc. next ( ) ) ;
Course course1 = new Course ( sc. next ( ) , sc. next ( ) ) ;
Course course2 = new Course ( sc. next ( ) , sc. next ( ) ) ;
Course course3 = new Course ( sc. next ( ) , sc. next ( ) ) ;
Course course4 = new Course ( sc. next ( ) , sc. next ( ) ) ;
Schedule schedule = new Schedule ( ) ;
schedule. addCourse ( student1, course2) ;
schedule. addCourse ( student1, course3) ;
schedule. addCourse ( student2, course1) ;
schedule. addCourse ( student3, course1) ;
schedule. addCourse ( student3, course2) ;
schedule. addCourse ( student3, course4) ;
System . out. println ( "学生总数为:" + Student . stuNum) ;
System . out. println ( "课程总数为:" + Course . cNum) ;
System . out. println ( "学生选课的总数为:" + Schedule . schNum) ;
schedule. displayCourse ( ) ;
}
}
7-14 成绩计算-2-关联类
import java. util. Scanner ;
import java. text. DecimalFormat ;
class Score {
private int pingshi;
private int qimo;
public Score ( int pingshi, int qimo) {
this . pingshi = pingshi;
this . qimo = qimo;
}
public Score ( ) {
}
public int getPingshi ( ) {
return pingshi;
}
public int getQimo ( ) {
return qimo;
}
public int getScore ( ) {
return ( int ) ( ( pingshi * 0.4 ) + ( qimo * 0.6 ) ) ;
}
}
class Student
{
private String num;
private String name;
private Score yuwen;
private Score shuxue;
private Score wuli;
public Student ( ) {
}
public Student ( String num, String name, Score yuwen, Score shuxue, Score wuli) {
this . num = num;
this . name = name;
this . yuwen = yuwen;
this . shuxue = shuxue;
this . wuli = wuli;
}
public void setNum ( String num)
{
this . num = num;
}
public String getNum ( ) {
return num;
}
public void setName ( String name)
{
this . name = name;
}
public String getName ( ) {
return name;
}
public void setYuwen ( Score yuwen)
{
this . yuwen = yuwen;
}
public Score getYuwen ( ) {
return yuwen;
}
public void setShuxue ( Score shuxue)
{
this . shuxue = shuxue;
}
public Score getShuxue ( ) {
return shuxue;
}
public void setWuli ( Score wuli)
{
this . wuli = wuli;
}
public Score getWuli ( ) {
return wuli;
}
public double qimoa ( ) {
return yuwen. getScore ( ) + shuxue. getScore ( ) + wuli. getScore ( ) ;
}
public double zuizhong ( int i) {
double all1 = yuwen. getPingshi ( ) + shuxue. getPingshi ( ) + wuli. getPingshi ( ) ;
double all2 = yuwen. getQimo ( ) + shuxue. getQimo ( ) + wuli. getQimo ( ) ;
double all3 = yuwen. getScore ( ) + shuxue. getScore ( ) + wuli. getScore ( ) ;
double lzy = 0 ;
if ( i== 0 ) {
lzy = ( double ) ( all1/ ( double ) 3 ) ;
return lzy;
}
else if ( i== 1 ) {
lzy = ( double ) ( all2/ ( double ) 3 ) ;
return lzy;
}
else {
lzy = ( double ) ( all3/ ( double ) 3 ) ;
return lzy;
}
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
Student [ ] student = new Student [ 3 ] ;
Score [ ] score = new Score [ 3 ] ;
String asd;
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 3 ; j++ ) {
student[ i] = new Student ( ) ;
student[ i] . setNum ( input. next ( ) ) ;
student[ i] . setName ( input. next ( ) ) ;
asd = input. next ( ) ;
Score chengji = new Score ( input. nextInt ( ) , input. nextInt ( ) ) ;
score[ j] = chengji;
student[ i] . setYuwen ( score[ 0 ] ) ;
student[ i] . setShuxue ( score[ 1 ] ) ;
student[ i] . setWuli ( score[ 2 ] ) ;
}
}
for ( int j= 0 ; j< 3 ; j++ )
if ( j< 2 ) System . out. println ( student[ j] . getNum ( ) + " " + student[ j] . getName ( ) + " " + ( int ) student[ j] . qimoa ( ) + " " + String . format ( "%.2f" , student[ j] . zuizhong ( 0 ) ) + " " + String . format ( "%.2f" , student[ j] . zuizhong ( 1 ) ) + " " + String . format ( "%.2f" , student[ j] . zuizhong ( 2 ) ) ) ;
else System . out. print ( student[ j] . getNum ( ) + " " + student[ j] . getName ( ) + " " + ( int ) student[ j] . qimoa ( ) + " " + String . format ( "%.2f" , student[ j] . zuizhong ( 0 ) ) + " " + String . format ( "%.2f" , student[ j] . zuizhong ( 1 ) ) + " " + String . format ( "%.2f" , student[ j] . zuizhong ( 2 ) ) ) ;
}
}
7-15 定义商品类,封装成员变量,输出对象
import java. util. Objects ;
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
import java. lang. Math ;
class Shops {
private String shopId;
private String shopName;
private Double shopMoney;
@Override
public String toString ( ) {
return
shopId + ',' + shopName + ',' + shopMoney;
}
Shops ( String shopId, String shopName, Double shopMoney) {
this . shopId = shopId;
this . shopName = shopName;
this . shopMoney = shopMoney;
}
public String getShopId ( ) {
return shopId;
}
public void setShopId ( String shopId) {
this . shopId = shopId;
}
public String getShopName ( ) {
return shopName;
}
public void setShopName ( String shopName) {
this . shopName = shopName;
}
public Double getShopMoney ( ) {
return shopMoney;
}
public void setShopMoney ( Double shopMoney) {
this . shopMoney = shopMoney;
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
Shops shops = new Shops ( sc. next ( ) , sc. next ( ) , sc. nextDouble ( ) ) ;
System . out. print ( shops. toString ( ) ) ;
}
}
7-16 设计一个BankAccount类
import java. util. Objects ;
import java. util. Scanner ;
import java. util. ArrayList ;
import java. util. List ;
import java. lang. Math ;
class BankAccount {
private int balance;
BankAccount ( ) {
this . balance = 0 ;
}
BankAccount ( int balance) {
this . balance = balance;
}
public int getBlance ( ) {
return this . balance;
}
public void withdraw ( int money) {
this . balance -= money;
}
public void deposit ( int money) {
this . balance += money;
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
BankAccount bank = new BankAccount ( ) ;
bank. deposit ( sc. nextInt ( ) ) ;
System . out. println ( bank. getBlance ( ) ) ;
bank. withdraw ( sc. nextInt ( ) ) ;
System . out. println ( bank. getBlance ( ) ) ;
bank. deposit ( sc. nextInt ( ) ) ;
System . out. println ( bank. getBlance ( ) ) ;
}
}