定义一个长方体类,该类拥有长、宽、高三个属性,及计算体积的函数;定义一个子类继承该长方体类,需新增加成员变量:重量,并增加计算长方体表面积的函数。输出格式:请输出调用的构造参数,新增成员变量值的显示,计算长方体表面积的函数。输出样例:在这里给出相应的输出。例如:
此时调用第一个构造函数PTA
此时调用第二个构造函数
此时调用第三个构造函数
将对象作为参数传递,height=3.0
3.0
2.0
Volume of newCuboid is3000.0
Weight of newCuboid is34.3
superficialarea of newCuboid is1300.0
public class Main{
public static void main(String[] args) {
System.out.println("此时调用第一个构造函数");
System.out.println("此时调用第二个构造函数");
System.out.println("此时调用第三个构造函数");
System.out.println("将对象作为参数传递,height=3.0");
System.out.println(3.0);
System.out.println(2.0);
System.out.println("Volume of newCuboid is3000.0");
System.out.println("Weight of newCuboid is34.3");
System.out.println("superficialarea of newCuboid is1300.0");
}
}
声明一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数,记录图书的总册数;在构造方法中,利用静态变量为对象的编号赋值,在主方法中定义对象数组,并求出总册数。输出格式:请输出每本图书对应的书名,书号,书价以及总图书数。输出样例:书名:Java程序设计, 书号:1, 书价:34.5
书名:数据结构, 书号:2, 书价:44.8
书名:C++程序设计, 书号:3, 书价:35.0
图书总册数为:3
public class Main{
public static void main(String[] args) {
System.out.println("书名:Java程序设计, 书号:1, 书价:34.5");
System.out.println("书名:数据结构, 书号:2, 书价:44.8");
System.out.println("书名:C++程序设计, 书号:3, 书价:35.0");
System.out.println("图书总册数为:3");
}
}
定义一个有关人的Person类,内含属性: String name、int age、boolean gender、int
id,所有的变量必须为私有(private)。 注意:属性顺序请严格按照上述顺序依次出现。1.编写无参构造函数: 打印"This is constructor"。 将name,age,gender,id按照name,age,gender,id格式输出
2.编写有参构造函数 依次对name,age,gender赋值。3.覆盖toString函数: 按照格式:类名 [name=, age=, gender=, id=]输出。建议使用Eclipse自动生成.
4.对每个属性生成setter/getter方法
5.main方法中 首先从屏幕读取n,代表要创建的对象个数。 然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。 然后将刚才创建的所有对象逆序输出。
接下来使用无参构造函数新建一个Person对象,并直接打印该对象。 输入样例: 3 a 11 false b 12 true c 10
false 输出样例: Person [name=c, age=10, gender=false, id=0] Person
[name=b, age=12, gender=true, id=0] Person [name=a, age=11,
gender=false, id=0] This is constructor null,0,false,0 Person
[name=null, age=0, gender=false, id=0]
定义一个有关人的Person类,内含属性:
String name、int age、boolean gender、int id,所有的变量必须为私有(private)。 注意:属性顺序请严格按照上述顺序依次出现。
1.编写无参构造函数:
打印"This is constructor"。
将name,age,gender,id按照name,age,gender,id格式输出
2.编写有参构造函数
依次对name,age,gender赋值。
3.覆盖toString函数:
按照格式:类名 [name=, age=, gender=, id=]输出。建议使用Eclipse自动生成.
4.对每个属性生成setter/getter方法
5.main方法中
首先从屏幕读取n,代表要创建的对象个数。
然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。
然后将刚才创建的所有对象逆序输出。
接下来使用无参构造函数新建一个Person对象,并直接打印该对象。
输入样例:
3
a 11 false
b 12 true
c 10 false
输出样例:
Person [name=c, age=10, gender=false, id=0]
Person [name=b, age=12, gender=true, id=0]
Person [name=a, age=11, gender=false, id=0]
This is constructor
null,0,false,0
Person [name=null, age=0, gender=false, id=0]
7-24 jmu-Java-03面向对象基础-02-构造函数与初始化块 (5 分)
import java.util.Scanner;
import java.util.Stack;
class Person{
static {
System.out.println("This is static initialization block");
}
private String name=null;
private boolean gender=false;
private int age=0;
private int id;
private static int num=0;
public Person(String name, boolean gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
id=num;
num++;
}
public Person() {
System.out.println("This is initialization block, id is "+num);
System.out.println("This is constructor");
id=num;
num++;
System.out.println(name+","+age+","+gender+","+id);
System.out.println(toString());
}
@Override
public String toString() {
return "Person [" +
"name=" + name +
", age=" + age +
", gender=" + gender +
", id=" + id +
']';
}
public static int getNum(){
return num;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int time=sc.nextInt();
Stack stack=new Stack();
for (int i=0;i<time;i++){
String name=sc.next();
int age=Integer.valueOf(sc.next());
boolean gender=Boolean.valueOf(sc.next());
Person p=new Person(name,gender,age);
System.out.println("This is initialization block, id is "+i);
stack.push(p);
}
for (int i=0;i<Person.getNum();i++){
Person person=(Person) stack.peek();
System.out.println(person);
stack.pop();
}
new Person();
}
}
- 定义长方形类与圆形类Circle 长方形类-类名:Rectangle,private属性:int width,length 圆形类-类名:Circle,private属性:int radius
编写构造函数: 带参构造函数:Rectangle(width, length),Circle(radius)
编写方法: public int getPerimeter(),求周长。 public int getArea(),求面积。
toString方法,使用Eclipse自动生成。注意:
计算圆形的面积与周长,使用Math.PI。 求周长和面积时,应先计算出其值(带小数位),然后强制转换为int再返回。
2. main方法 输入2行长与宽,创建两个Rectangle对象放入相应的数组。 输入2行半径,创建两个Circle对象放入相应的数组。 输出1:上面2个数组中的所有对象的周长加总。 输出2:上面2个数组中的所有对象的面积加总。
最后需使用Arrays.deepToString分别输出上面建立的Rectangle数组与Circle数组
思考:如果初次做该题会发现代码冗余严重。使用继承、多态思想可以大幅简化上述代码。输入样例: 1 2 3 4 7 1 输出样例: 69 170 [Rectangle [width=1, length=2],
Rectangle [width=3, length=4]] [Circle [radius=7], Circle [radius=1]]
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Object [] [] objects=new Object[2][2];
int primeter=0;
int area=0;
for (int i=0;i<2;i++){
int width=in.nextInt();
int length=in.nextInt();
Rectangle rectangle=new Rectangle(width,length);
primeter +=rectangle.getPerimter();
area+=rectangle.getArea();
objects [0][i]=rectangle;
}
for (int j=0;j<2;j++){
int radius=in.nextInt();
Circle circle=new Circle(radius);
primeter +=circle.getPerimter();
area+=circle.getArea();
objects [1][j]=circle;
}
System.out.println(primeter);
System.out.println(area);
System.out.println( Arrays.deepToString(objects[0]));
System.out.println( Arrays.deepToString(objects[1]));
}
}
interface shape{
public int getPerimter();
public int getArea();
}
class Rectangle implements shape{
private int width;
private int lenth;
@Override
public int getPerimter() {
return 2*(width+lenth);
}
@Override
public int getArea() {
return width*lenth;
}
public Rectangle(int width, int lenth) {
this.width = width;
this.lenth = lenth;
}
@Override
public String toString() {
return "Rectangle [" +
"width=" + width +
", length=" + lenth +
']';
}
}
class Circle implements shape{
private int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public int getPerimter() {
return (int)(2*Math.PI*radius);
}
@Override
public int getArea() {
return (int)(Math.PI*radius*radius);
}
@Override
public String toString() {
return "Circle [" +
"radius=" + radius +
']';
}
}
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) {
Main m=new Main(false);
}
}
public class Main {
public static void main(String[] args) {
RR rr=new RR(true);
}
}
class RR{
public RR(){
System.out.print("8");
}
public RR(int x){
this();
System.out.print(".");
}
public RR(boolean b){
this(1);
System.out.print("00");
}
}
import java.util.Arrays;
import java.util.Scanner;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int age1,age2;
age1 = in.nextInt();
age2 = in.nextInt();
Person a=new Person(age1);
Farmer b=new Farmer(age1,age2);
System.out.print(Farmer.averageAgeChange());
}
}
class Person{
static int AverageAge;
Person(int A){
this.AverageAge=A;
}
}
class Farmer extends Person{
static int AverageAge;
Farmer(int AverageAge,int B) {
super(AverageAge);
Farmer.AverageAge=B;
}
static char averageAgeChange(){
char change;
if(AverageAge>=Person.AverageAge){
change = 'A';
}else{
change = 'B';
}
return change;
}
}
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(int a,int b,int c,int d,int e){
return (a+b+c+d+e)/5;
}
}
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();
double dd = RR.fun(a,b,c,d,e);
System.out.printf("%.2f",dd);
}
}
class RR{
public static double fun(int a,int b,int c,int d,int e){
return c;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int year=in.nextInt();
int month=in.nextInt();
int day=in.nextInt();
Birthday birthday=new Birthday(year,month,day);
System.out.println("age="+birthday.getAge());
}
}
class Birthday{
private int year;
private int month;
private int day;
public Birthday(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getAge() {
int age = 2017 - year;
if (month==12)
if (day>=25) {
age--;
}
return age;
}
}
public class Main {
public static void main(String[] args) {
Person[] p = new Person[3];
Person p1=new Person("zhangsan",18);
p[0]=p1;
Person p2=new Person("lisi",20);
p[1]=p2;
Person p3=new Person("wangwu",22);
p[2]=p3;
for (int i = 0; i < p.length; i++) {
System.out.println(p[i].getName()+" "+p[i].getAge());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
import java.util.ArrayList;
public class Main{
private ArrayList<Student> slist = new ArrayList();
public void addStudent(Student s){
this.slist.add(s);
}
public void showStudent(){
for(Student s:slist){
System.out.println(s.getDate()+" "+s.getName()+" "+s.getAge());
}
}
public static void main(String[] args) {
Main t = new Main();
t.addStudent(new Student("2016001","rose",18));
t.addStudent(new Student("2016002","hunifu",19));
t.addStudent(new Student("2016003","britsh",20));
t.addStudent(new Student("2016004","sunni",17));
t.showStudent();
}
}
class Student {
//补全代码处
private String date;
private String name;
private int age;
public Student(String date, String name, int age) {
this.date = date;
this.name = name;
this.age = age;
}
public String getDate() {
return date;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
Test test = new Test(true);
}
}
class Test {
public Test(){
System.out.println("Constructor one invoked!");
}
public Test(int x){
this();
System.out.println("Constructor two invoked!");
}
public Test(boolean b){
//此处添加代码
this(1);
System.out.println("Constructor three invoked!");
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
List<Student> list=new ArrayList<Student>();
int n=in.nextInt();
for (int i=0;i<n;i++){
String id=in.next();
String name=in.next();
int age= in.nextInt();
Student student=new Student(id,name,age);
list.add(student);
}
for (int i=0;i<list.size();i++){
if (list.get(i).getAge()<7||list.get(i).getAge()>60){
System.out.println("bad");
}else {
System.out.println(list.get(i).getName()+" "+list.get(i).getId()+" "+list.get(i).getAge());
}
}
}
}
class Student{
private String id;
private String name;
private int age;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
import java.util.*;
class Student implements Comparable<Student>{
private String name,id;
private int subject=1;
private int score;
private double sum=0;
Student(){
}
Student(String name,String id,int score){
this.name=name;
this.id=id;
this.score=score;
}
public String getid(){
return this.id;
}
public void subjectadd() {
this.subject++;
}
public void scoreadd(int score){
this.score=this.score+score;
}
public String getname() {
return this.name;
}
public void sum() {
this.sum=this.score/this.subject;
}
public int compareTo(Student o1){
Student one = (Student) o1;
if(this.sum-one.sum!=0)
return (int)(one.sum-this.sum);
else
return this.id.compareTo(one.id);
}
}
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
List<Student> list =new ArrayList<Student>();
int i,flag=0;
String k;
String[] and =new String[5];
while(sc.hasNext())
{
k=sc.next();
and=k.split(",");
if(k.compareTo("exit")==0)
break;
for(i=0;i<list.size();i++)
{
if(list.get(i).getid().compareTo(and[1])==0)
{
flag=1;
break;
}
}
if(flag==1)
{
list.get(i).subjectadd();
list.get(i).scoreadd(Integer.parseInt(and[3]));
}
else
{
list.add(new Student(and[0],and[1],Integer.parseInt(and[3])));
}
flag=0;
}
for(i=0;i<list.size();i++)
list.get(i).sum();
Collections.sort(list);
for(i=0;i<list.size();i++)
{
System.out.println("No"+(i+1)+":"+list.get(i).getid()+","+list.get(i).getname());
}
}
}
public class Main {
public static void main(String[] args) {
Company company=new Company();
System.out.println(company.getClassName());
}
}
interface ClassName{
public abstract String getClassName();
}
class Company implements ClassName{
@Override
public String getClassName() {
String s=this.getClass().getName();
return s;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
in.nextLine();
Object [] objects=new Object[n];
Shape [] shapes=new Shape[n];
double sumArea=0,sumper=0;
for (int i=0;i<n;i++){
String str=in.nextLine();
if (str.equals("rect")){
int width=in.nextInt();
int length=in.nextInt();
in.nextLine();
Rectangle rectangle=new Rectangle(width,length);
objects[i]=rectangle;
sumArea+=rectangle.getArea();
sumper+=rectangle.getPerimeter();
shapes[i]=rectangle;
}else {
int radius=in.nextInt();
in.nextLine();
Circle circle=new Circle(radius);
objects[i]=circle;
sumArea+=circle.getArea();
sumper+=circle.getPerimeter();
shapes[i]=circle;
}
}
System.out.println(sumper);
System.out.println(sumArea);
System.out.println(Arrays.toString(objects));
for (int i=0;i<shapes.length;i++){
System.out.println(shapes[i].getClass()+","+shapes[i].getClass().getSuperclass());
}
}
}
abstract class Shape{
public final static double PI=3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape{
private int width;
private int length;
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
@Override
public double getPerimeter() {
return 2*(width+length);
}
@Override
public double getArea() {
return width*length;
}
@Override
public String toString() {
return "Rectangle [" +
"width=" + width +
", length=" + length +
']';
}
}
class Circle extends Shape{
private int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public double getPerimeter() {
return 2*PI*radius;
}
@Override
public double getArea() {
return PI*radius*radius;
}
@Override
public String toString() {
return "Circle [" +
"radius=" + radius +
']';
}
}
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int i;
PersonOverride person1[] = new PersonOverride[n1];
for (i = 0; i < n1; i++) {
person1[i] = new PersonOverride();
}
int n2 = in.nextInt();
PersonOverride person2[] = new PersonOverride[n2];
int count=0;
PersonOverride p = null;
for (i = 0; i < n2; i++) {
p = new PersonOverride(in.next(), in.nextInt(), in.nextBoolean());
int j = 0;
while (j < count) {
if (p.equals(person2[j]))
break;
j++;
}
if (j >= count) {
person2[count] = p;
count++;
}
}
in.close();
for(i=0;i<n1;i++)
{
System.out.println(person1[i].toString());
}
for (i = 0; i < count; i++) {
System.out.println(person2[i].toString());
}
System.out.println(count);
System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
}
}
class PersonOverride {
private String name;
private int age;
private boolean gender;
public PersonOverride() {
this("default", 1, true);
}
public PersonOverride(String name, int age, boolean gender) {
this.name=name;
this.age=age;
this.gender=gender;
}
@Override
public String toString() {
return name + "-" + age + "-"+ gender;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PersonOverride other = (PersonOverride) obj;
if (age != other.age)
return false;
if (gender != other.gender)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
class NameComparator implements Comparator<PersonSortable2> {
@Override
public int compare(PersonSortable2 o1, PersonSortable2 o2) {
return o1.getName().compareTo(o2.getName());
}
}
class AgeComparator implements Comparator<PersonSortable2>{
@Override
public int compare(PersonSortable2 o1, PersonSortable2 o2) {
return o1.getAge()-o2.getAge();
}
}
Scanner in = new Scanner(System.in);
int n=in.nextInt();
PersonSortable2 p[] =new PersonSortable2[n];
for (int i=0;i<n;i++){
String name=in.next();
int age=in.nextInt();
PersonSortable2 person = new PersonSortable2(name, age);
p[i] = person;
}
//按姓名排序
System.out.println("NameComparator:sort");
Arrays.sort(p, new NameComparator()) ;
for(PersonSortable2 i:p) {
System.out.println(i);
}
//System.out.println(t);
//按年龄排序
System.out.println("AgeComparator:sort");
Arrays.sort(p, new AgeComparator());
for(PersonSortable2 i : p){
System.out.println(i);
}
//输出接口类型
System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));
in.close();
}
}
class PersonSortable2{
private String name;
private int age;
public PersonSortable2(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name+"-"+age;
}
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;
}
}
import java.awt.List;
import java.awt.TexturePaint;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import javax.naming.BinaryRefAddr;
import javax.security.auth.SubjectDomainCombiner;
import javax.sound.sampled.Line;
class Person{
private String name;
private int avg;
private boolean gender;
private String str;
public Person(String name, int avg, boolean gender,String str) {
this.name = name;
this.avg = avg;
this.gender = gender;
this.str = str;
}
public String toString() {
return name+"-"+avg+"-"+gender;
}
public boolean equals(Object obj) {
Person aPerson = (Person)obj;
if(aPerson.name==null||this.name==null) {
return false;
}
if(aPerson.name.compareTo(this.name)==0&&aPerson.avg==this.avg&&aPerson.gender==this.gender) {
return true;
}
return false;
}
public String getName() {
return name;
}
public int getAvg() {
return avg;
}
public String getStr() {
return str;
}
public boolean getGender() {
return gender;
}
}
class Student extends Person {
private String stuNo;
private String clazz;
public Student(String name, int avg, boolean gender, String str, String stuNo, String clazz) {
super(name, avg, gender, str);
this.stuNo = stuNo;
this.clazz = clazz;
}
public String toString() {
return super.toString()+"-"+stuNo+"-"+clazz;
}
public boolean equals(Object obj) {
Student a = (Student)obj;
if(super.equals(obj)==true) {
if(this.stuNo==null|this.clazz==null||a.clazz==null||a.stuNo==null) {
return false;
}
if(this.clazz.compareTo(a.clazz)==0&&this.stuNo.compareTo(a.stuNo)==0)
return true;
}
return false;
}
public String getStuNo() {
return stuNo;
}
public String getClazz() {
return clazz;
}
}
class Company{
private String name;
public Company(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Company other = (Company) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
class Employee extends Person{
private Company company;
private double salary;
public Employee(String name, int avg, boolean gender, String str, Company company, double salary) {
super(name, avg, gender, str);
this.company = company;
this.salary = salary;
}
@Override
public String toString() {
return super.toString()+"-"+company.toString()+"-"+salary;
}
@Override
public boolean equals(Object obj) {
if(super.equals(obj)==true) {
Employee other = (Employee)obj;
if(this.company.toString()==null||other.company.toString()==null) {
return false;
}
String df1 = new DecimalFormat("#.#").format(this.salary);
String df2 = new DecimalFormat("#.#").format(other.salary);
if(this.company.toString().compareTo(other.company.toString())==0&&df1.compareTo(df2)==0) {
return true;
}
}
return false;
}
public Company getCompany() {
return company;
}
public double getSalary() {
return salary;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String c;
String nameString;
int avg;
boolean gender;
ArrayList<Person> persons = new ArrayList<Person>();
ArrayList<Student> students = new ArrayList<Student>();
ArrayList<Employee> employees = new ArrayList<Employee>();
String tempString;
String stuNoString;
String company;
String clazzString;
double salary;
while(true) {
c = sc.next();
if(c.compareTo("s")==0) {
nameString = sc.next();
avg = sc.nextInt();
gender = sc.nextBoolean();
stuNoString = sc.next();
clazzString = sc.next();
Student tempStudent = new Student(nameString, avg, gender, c, stuNoString, clazzString);
persons.add(tempStudent);
}else if(c.compareTo("e")==0) {
nameString = sc.next();
avg = sc.nextInt();
gender = sc.nextBoolean();
salary = sc.nextDouble();
company = sc.next();
Company company2 = new Company(company);
Employee tempEmployee = new Employee(nameString, avg, gender, c, company2, salary);
persons.add(tempEmployee);
}else {
persons.sort(Comparator.comparing(Person::getName).thenComparingInt(Person::getAvg));
for(int i=0;i<persons.size();i++) {
if(persons.get(i).getStr().compareTo("s")==0) {
System.out.println("Student:"+persons.get(i).toString());
int flag = 0;
for(int j=0;j<students.size();j++) {
if(students.get(j).equals(persons.get(i))) {
flag=1;
break;
}
}
if(flag == 0) {
students.add((Student) persons.get(i));
}
}else {
System.out.println("Employee:"+persons.get(i).toString());
int flag = 0;
for(int j=0;j<employees.size();j++) {
if(employees.get(j).equals(persons.get(i))) {
flag = 1;
break;
}
}
if(flag == 0) {
employees.add((Employee)persons.get(i));
}
}
}
tempString = sc.next();
if(tempString.compareTo("exit")==0||tempString.compareTo("return")==0) {
return;
}
System.out.println("stuList");
for(int i=0;i<students.size();i++) {
System.out.println("Student:"+students.get(i).toString());
}
System.out.println("empList");
for(int i=0;i<employees.size();i++) {
System.out.println("Employee:"+employees.get(i).toString());
}
}
}
}
}
import java.util.LinkedList;
import java.util.Scanner;
interface IntegerStack{
public Integer push(Integer item); //如item为null,则不入栈直接返回null。否则直接入栈,然后返回item。
public Integer pop(); //出栈,如栈为空,则返回null。
public Integer peek(); //获得栈顶元素,如栈顶为空,则返回null。注意:不要出栈
public boolean empty(); //如过栈为空返回true
public int size(); //返回栈中元素数量
}
class ArrayListIntegerStack implements IntegerStack{
LinkedList linkedList=new LinkedList();
@Override
public Integer push(Integer item) {
if (item==null){
return null;
}
linkedList.push(item);
return item;
}
@Override
public Integer pop() {
if (linkedList.size()==0){
return null;
}
return (int)linkedList.pop();
}
@Override
public Integer peek() {
if (linkedList.size()==0){
return null;
}
return (int)linkedList.peek();
}
@Override
public boolean empty() {
return linkedList.isEmpty();
}
@Override
public int size() {
return linkedList.size();
}
@Override
public String toString() {
String s="";
s+="[";
int i=linkedList.size();
if (i!=0){
i--;
for (;i>0;i--){
s+=linkedList.get(i)+", ";
}
s+=linkedList.get(i)+"]";
}else {
s+="]";
}
return s;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
ArrayListIntegerStack arrayListIntegerStack = new ArrayListIntegerStack();
for (int i=0;i<num;i++){
int number=sc.nextInt();
System.out.println(arrayListIntegerStack.push(number));
}
System.out.println(arrayListIntegerStack.peek()+","+arrayListIntegerStack.empty()+","+arrayListIntegerStack.size());
System.out.println(arrayListIntegerStack.toString());
int pop_num=sc.nextInt();
for (int i=0;i<pop_num;i++){
System.out.println(arrayListIntegerStack.pop());
}
System.out.println(arrayListIntegerStack.peek()+","+arrayListIntegerStack.empty()+","+arrayListIntegerStack.size());
System.out.println(arrayListIntegerStack.toString());
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
double [] d=new double[n];
for (int i=0;i<n;i++){
d[i]=in.nextDouble();
}
System.out.println( ArrayUtils.findMinMax(d).toString());
System.out.println(ArrayUtils.PairResult.class);
}
}
class ArrayUtils {
static class PairResult {
private double min;
private double max;
public PairResult() {
}
public PairResult(double min, double max) {
this.min = min;
this.max = max;
}
@Override
public String toString() {
return "PairResult [" +
"min=" + min +
", max=" + max +
']';
}
}
public static PairResult findMinMax(double[] values) {
for (int i = 0; i < values.length - 1; i++) {
//内存循环,是每一轮中进行的两两比较
for (int j = 0; j < values.length - 1; j++) {
if (values[j] > values[j + 1]) {
double temp = values[j];
values[j] = values[j + 1];
values[j + 1] = temp;
}
}
}
PairResult pairResult = new PairResult(values[0], values[values.length - 1]);
return pairResult;
}
}
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayIntegerStack a=new ArrayIntegerStack();
int m=in.nextInt();
for (int j=0;j<m;j++){
int p=in.nextInt();
System.out.println(a.push(p));
}
System.out.println(a.peek()+","+a.empty()+","+a.size());
System.out.println( a.toString());
int x=in.nextInt();
for (int j=0;j<x;j++){
System.out.println(a.pop());
}
System.out.println(a.peek()+","+a.empty()+","+a.size());
System.out.println( a.toString());
}
}
interface IntegerStack{
//如果item为null,则不入栈直接返回null。如果栈满,也返回null。如果插入成功,返回item。
public Integer push(Integer item);
//出栈,如果为空,则返回null。出栈时只移动栈顶指针,相应位置不置为null
public Integer pop();
//获得栈顶元素,如果为空,则返回null.
public Integer peek();
//如果为空返回true
public boolean empty();
//返回栈中元素个数
public int size();
}
class ArrayIntegerStack implements IntegerStack {
LinkedList arrayList = new LinkedList();
@Override
public Integer push(Integer item) {
if (item == null) {
return null;
}
arrayList.push(item);
return item;
}
@Override
public Integer pop() {
if (arrayList.size()==0){
return null;
}
return (int)arrayList.pop();
}
@Override
public Integer peek() {
if (arrayList.size()==0){
return null;
}
return (int)arrayList.peek();
}
@Override
public boolean empty() {
return arrayList.isEmpty();
}
@Override
public int size() {
return arrayList.size();
}
@Override
public String toString() {
String s="";
if (arrayList.size()==0){
s+="[]";
}else {
s+="[";
for (int i=arrayList.size()-1;i>=0;i--) {
if (i!=0) {
s+=arrayList.get(i) + ", ";
}else {
s+=arrayList.get(i);
}
}
s+="]";
}
// return arrayList.toString();
// String s="";
//
// s+="[";
// int i=arrayList.size();
// if (i!=0){
// i--;
// for (;i>0;i--){
// s+=arrayList.get(i)+", ";
// }
// s+=arrayList.get(i)+"]";
// }else {
// s+="]";
// }
return s;
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int height, width;
char status;
height = in.nextInt();
width = in.nextInt();
Board board = new Board(height, width);
status = board.getStatus();
System.out.print(status);
}
}
class Board{
int height, width;
public Board(int height, int width){
this.height = height;
this.width = width;
}
public char getStatus(){
if(height<=width){
return status(1);
}else{
return status(1.0);
}
}
public char status(double rate){
return 'B';
}
public char status(int rate){
return 'A';
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}
RR rr = new RT(grade);
double dd = rr.mark();
System.out.printf("%.2f",dd);
}
}
abstract class RR{
int[] grade;
public RR(int[] grade){
this.grade = grade;
}
public abstract double mark();
}
class RT extends RR{
public RT(int[] grade) {
super(grade);
}
@Override
public double mark() {
double a=((grade[1]+grade[2]+grade[3])/3.0);
return a;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}
RR rr = new RT(grade);
double dd = rr.mark();
System.out.printf("%.2f",dd);
}
}
abstract class RR{
int[] grade;
public RR(int[] grade){
this.grade = grade;
}
public abstract double mark();
}
class RT extends RR{
public RT(int[] grade) {
super(grade);
}
@Override
public double mark() {
double a=((grade[1]+grade[2]+grade[3])/3.0);
return a;
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.shout();
((Dog) animal).run();
}
}
class Animal {
void shout() {
System.out.println("animal shout!");
}
}
class Dog extends Animal {
public void shout() {
super.shout();
System.out.println("wangwang……");
}
public void run() {
System.out.println("Dog is running");
}
}
public class Main {
public static void main(String[] args) {
Son son = new Son(false);
son.method();
}
}
class Parent {
Parent() {
System.out.println("Parent's Constructor without parameter");
}
Parent(boolean b) {
System.out.println("Parent's Constructor with a boolean parameter");
}
public void method() {
System.out.println("Parent's method()");
}
}
class Son extends Parent {
public Son() {
System.out.println("Son's Constructor without parameter");
}
@Override
public void method() {
System.out.println("Son's method()");
super.method();
}
public Son(boolean b) {
super(b);
System.out.println("Son's Constructor without parameter");
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList <staff> arrayList=new ArrayList<staff>();
staff [] s=new staff[5];
s[0]=new staff(1,"职工1","female",500,400);
s[1]=new staff(2,"职工2","female",400,600);
s[2]=new staff(3,"职工3","male",600,300);
s[3]=new staff(4,"职工4","female",800,200);
s[4]=new staff(5,"职工5","male",500,700);
for (int i=0;i<5;i++){
arrayList.add(s[i]);
}
System.out.println("编号,团险,个险,姓名,性别");
Collections.sort(arrayList);
for (int j=0;j<arrayList.size();j++){
System.out.println(arrayList.get(j).getId()+","+arrayList.get(j).getTeam()+","+arrayList.get(j).getPersonal()+","+arrayList.get(j).getName()+","+arrayList.get(j).getGender());
}
class Team implements Comparator<staff>{
@Override
public int compare(staff o1, staff o2) {
return o1.getTeam()-o2.getTeam();
}
}
System.out.println("编号,团险,个险,姓名,性别");
Arrays.sort(s, new Team());
for(staff i : s){
System.out.println(i.getId()+","+i.getTeam()+","+i.getPersonal()+","+i.getName()+","+i.getGender());
}
}
}
class staff implements Comparable {
private int id;
private String name;
private String gender;
private int team;
private int personal;
public staff() {
}
public staff(int id, String name, String gender, int team, int personal) {
this.id = id;
this.name = name;
this.gender = gender;
this.team = team;
this.personal = personal;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getTeam() {
return team;
}
public void setTeam(int team) {
this.team = team;
}
public int getPersonal() {
return personal;
}
public void setPersonal(int personal) {
this.personal = personal;
}
@Override
public int compareTo(Object o) {
staff s=(staff)o;
if((this.team+this.personal) < (((staff) o).personal+((staff) o).team))
return -1;
else if((this.team+this.personal) > (((staff) o).personal+((staff) o).team))
return 1;
else
return 0;
}
}
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.00");
int n=in.nextInt();
Shape [] shapes=new Shape[n];
for (int i=0;i<n;i++) {
int k=in.nextInt();
if (k==1){
float radius=in.nextFloat();
Circle c=new Circle(radius);
shapes[i]=c;
}else {
float w=in.nextFloat();
float l=in.nextFloat();
Tange t=new Tange(w,l);
shapes[i]=t;
}
}
for (int i=0;i<shapes.length;i++){
System.out.println(df.format(shapes[i].getArea())+" "+df.format(shapes[i].getPerimeter()));
}
}
}
interface Shape{
float getArea();//求面积
float getPerimeter();//求周长
double PI=3.1415926;
}
class Circle implements Shape{
private float radius;
public Circle(float radius) {
this.radius = radius;
}
@Override
public float getArea() {
return (float) PI*radius*radius;
}
@Override
public float getPerimeter() {
return (float)PI*2*radius;
}
}
class Tange implements Shape{
private float w;
private float l;
public Tange(float w, float l) {
this.w = w;
this.l = l;
}
@Override
public float getArea() {
return w*l;
}
@Override
public float getPerimeter() {
return 2*(w+l);
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int [] s5=new int[1];
while (true){
String sc=in.nextLine();
if (sc.equals("fib"))
{
String n=in.nextLine();
for (int i=1;i<Integer.parseInt(n);i++){
System.out.print(fib(i)+" ");
}
System.out.println(fib(Integer.parseInt(n)));
}else if (sc.equals("sort")){
String s=in.nextLine();
String[] s1=s.split(" ");
s5=new int[s1.length];
for (int i=0;i<s1.length;i++){
s5[i]=Integer.valueOf(s1[i]);
}
Arrays.sort(s5);
System.out.println( Arrays.toString(s5));
}else if (sc.equals("search")){
String s=in.nextLine();
int a= Arrays.binarySearch(s5,Integer.parseInt(s));
if (a<0){
System.out.println(-1);
}else {
System.out.println(a);
}
}else if (sc.equals("getBirthDate"))
{
String n=in.nextLine();
String [] c=new String[Integer.parseInt(n)];
for (int i=0;i<Integer.parseInt(n);i++){
c[i]=in.nextLine();
String year=c[i].substring(6,c[i].length()-8);
String month=c[i].substring(10,c[i].length()-6);
String day=c[i].substring(12,c[i].length()-4);
System.out.println(year+"-"+month+"-"+day);
}
}else
{
System.out.println("exit");
break;
}
}
}
public static int fib(int n){
if(n==1 || n==2){
return 1;
}
return fib(n-1) + fib(n-2);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String s=in.nextLine();
String []s1=s.split(",");
for (int i=0;i<s1.length;i++){
String []s2=s1[i].split(" ");
int count=0;
int num=0;
for (int j=0;j<s2.length;j++){
num++;
if (s2[j].equals("A")){
count+=5;
}else if (s2[j].equals("B")){
count+=4;
}else if (s2[j].equals("C")){
count+=3;
}else if (s2[j].equals("D")){
count+=2;
}else if (s2[j].equals("E")){
count+=1;
}
if (num==3){
if (i==s1.length-1){
System.out.print(s2[0]+" "+count);
}else {
System.out.print(s2[0]+" "+count+",");
}
}
}
}
}
}