JAVA初级编程。。很简单。希望是原创的越短越简单越好,写的好有追加分!! 200
设计一个person类,有名字,性别,年龄等属性,定义构造函数用来初始化类的这些属性,定义toString()方法输出Person的信息设计一个Student类,该类继承...
设计一个person类,有名字,性别,年龄等属性,定义构造函数用来初始化类的这些属性,定义toString()方法输出Person的信息
设计一个Student类,该类继承Person,并且在Student类中添加一个用于表示学生学号的(sn)属性,定义构造函数用来初始化类的这些属性,定义toString()方法输出Student的信息
定义一个班级类,有班级名称,班主任(Person的对象),学生(Student类的对象),定义构造函数用来初始化类的这些属性,定义toString方法输出班级的信息,编写应用程序使用该类
编写java应用程序,实现简单的加法功能,在计算器窗口界面中,包含一个文本框,0-9,+,=。只用实现加法就可以 展开
设计一个Student类,该类继承Person,并且在Student类中添加一个用于表示学生学号的(sn)属性,定义构造函数用来初始化类的这些属性,定义toString()方法输出Student的信息
定义一个班级类,有班级名称,班主任(Person的对象),学生(Student类的对象),定义构造函数用来初始化类的这些属性,定义toString方法输出班级的信息,编写应用程序使用该类
编写java应用程序,实现简单的加法功能,在计算器窗口界面中,包含一个文本框,0-9,+,=。只用实现加法就可以 展开
展开全部
第一题:
public class Test {//测试类
public static void main(String[] args) {
Person bzr=new Person("班主任1","男",50);//班主任
Student[] students=new Student[9];//9个学生
for (int i = 0; i < 9; i++) {//初始化9个学生
if (i%2==0) {
students[i]=new Student("学生"+(i+1),"男",12,"00"+(i+1));
}else {
students[i]=new Student("学生"+(i+1),"女",12,"00"+(i+1));
}
}
Cls cls=new Cls("一班",bzr,students);//班级
System.out.println(cls);//打印班级
}
}
class Person{//一个person类
String name;//姓名
String sex;//性别
int age;//年龄
public Person(String name, String sex, int age) {//定义构造函数用来初始化类的这些属性
this.name = name;
this.sex = sex;
this.age = age;
}
public Person() {
}
public String toString() {//toString()方法输出Person的信息
return "姓名:"+name+"\t性别:"+sex+"\t年龄:"+age;
}
}
class Student extends Person{//一个Student类,该类继承Person
String sn;//添加一个用于表示学生学号的(sn)属性
public Student(String name, String sex, int age, String sn) {//定义构造函数用来初始化类的这些属性
super(name, sex, age);
this.sn = sn;
}
public Student(){}
public String toString() {//定义toString()方法输出Student的信息
return super.toString()+"\t学号:"+sn;
}
}
class Cls{//一个班级类
String name;//班级名称
Person bzr;//班主任(Person的对象)
Student[] students;//学生(Student类的对象)
public Cls(String name, Person bzr, Student[] students) {
this.name = name;
this.bzr = bzr;
this.students = students;
}
public Cls(){};
public String toString() {//定义toString方法输出班级的信息
String s= "班级名称:\n"+name+"\n班主任:\n"+bzr+"\n";
for (Student student : students) {
s+="学生:\n"+student+"\n";
}
return s;
}
}
第二题:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalculatorGUI {
private Frame f;
private Panel p1, p2;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button bAdd,bCal;
private TextField tf;
private String s, op;
private Calculator cal = new Calculator();
private boolean ifOp;
public CalculatorGUI() {
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bAdd = new Button("+");
bCal = new Button("=");
tf = new TextField(25);
tf.setEditable(false);
}
public void launchFrame() {
f.setSize(220, 160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1, BorderLayout.NORTH);
p2.setLayout(new GridLayout(4, 4));
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b0);
p2.add(bAdd);
p2.add(bCal);
f.add(p2, BorderLayout.SOUTH);
f.setVisible(true);
}
public void setTextFieldText_Temp() {
if (tf.getText().length() < 15
&& (tf.getText().indexOf(".") == -1 || !s.equals("."))) {
tf.setText(tf.getText() + s);
} else {
tf.setText((tf.getText() + s).substring(0, 15));
}
}
public void setTextFieldText() {
if (ifOp) {
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
} else {
setTextFieldText_Temp();
}
}
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}
class myWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class setLabelText_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}
class setOperator_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
op = tempB.getLabel();
if (op.equals("+")) {
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
} else if (op.equals("=")) {
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
class Calculator {
private String result = "0";
private int op = 0, add = 1;
private double stringToDouble(String x) {
double y = Double.parseDouble(x);
return y;
}
private void operate(String x) {
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op) {
case 0:
result = x;
break;
case 1:
result = String.valueOf(y + x1);
break;
}
}
public String opAdd(String x) {
operate(x);
op = add;
return result;
}
public String opEquals(String x) {
operate(x);
op = 0;
return result;
}
public void opClean() {
op = 0;
result = "0";
}
}
public class Test {//测试类
public static void main(String[] args) {
Person bzr=new Person("班主任1","男",50);//班主任
Student[] students=new Student[9];//9个学生
for (int i = 0; i < 9; i++) {//初始化9个学生
if (i%2==0) {
students[i]=new Student("学生"+(i+1),"男",12,"00"+(i+1));
}else {
students[i]=new Student("学生"+(i+1),"女",12,"00"+(i+1));
}
}
Cls cls=new Cls("一班",bzr,students);//班级
System.out.println(cls);//打印班级
}
}
class Person{//一个person类
String name;//姓名
String sex;//性别
int age;//年龄
public Person(String name, String sex, int age) {//定义构造函数用来初始化类的这些属性
this.name = name;
this.sex = sex;
this.age = age;
}
public Person() {
}
public String toString() {//toString()方法输出Person的信息
return "姓名:"+name+"\t性别:"+sex+"\t年龄:"+age;
}
}
class Student extends Person{//一个Student类,该类继承Person
String sn;//添加一个用于表示学生学号的(sn)属性
public Student(String name, String sex, int age, String sn) {//定义构造函数用来初始化类的这些属性
super(name, sex, age);
this.sn = sn;
}
public Student(){}
public String toString() {//定义toString()方法输出Student的信息
return super.toString()+"\t学号:"+sn;
}
}
class Cls{//一个班级类
String name;//班级名称
Person bzr;//班主任(Person的对象)
Student[] students;//学生(Student类的对象)
public Cls(String name, Person bzr, Student[] students) {
this.name = name;
this.bzr = bzr;
this.students = students;
}
public Cls(){};
public String toString() {//定义toString方法输出班级的信息
String s= "班级名称:\n"+name+"\n班主任:\n"+bzr+"\n";
for (Student student : students) {
s+="学生:\n"+student+"\n";
}
return s;
}
}
第二题:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalculatorGUI {
private Frame f;
private Panel p1, p2;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button bAdd,bCal;
private TextField tf;
private String s, op;
private Calculator cal = new Calculator();
private boolean ifOp;
public CalculatorGUI() {
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bAdd = new Button("+");
bCal = new Button("=");
tf = new TextField(25);
tf.setEditable(false);
}
public void launchFrame() {
f.setSize(220, 160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1, BorderLayout.NORTH);
p2.setLayout(new GridLayout(4, 4));
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b0);
p2.add(bAdd);
p2.add(bCal);
f.add(p2, BorderLayout.SOUTH);
f.setVisible(true);
}
public void setTextFieldText_Temp() {
if (tf.getText().length() < 15
&& (tf.getText().indexOf(".") == -1 || !s.equals("."))) {
tf.setText(tf.getText() + s);
} else {
tf.setText((tf.getText() + s).substring(0, 15));
}
}
public void setTextFieldText() {
if (ifOp) {
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
} else {
setTextFieldText_Temp();
}
}
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}
class myWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class setLabelText_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}
class setOperator_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
op = tempB.getLabel();
if (op.equals("+")) {
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
} else if (op.equals("=")) {
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
class Calculator {
private String result = "0";
private int op = 0, add = 1;
private double stringToDouble(String x) {
double y = Double.parseDouble(x);
return y;
}
private void operate(String x) {
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op) {
case 0:
result = x;
break;
case 1:
result = String.valueOf(y + x1);
break;
}
}
public String opAdd(String x) {
operate(x);
op = add;
return result;
}
public String opEquals(String x) {
operate(x);
op = 0;
return result;
}
public void opClean() {
op = 0;
result = "0";
}
}
展开全部
package com.xtgj.p1;
public class person {
protected String name;
protected String sex;
protected int age;
public person() {
super();
// TODO Auto-generated constructor stub
}
public person(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.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 void showName(){
System.out.println(this.name+"\t");
}
}
package com.xtgj.p1;
public class student extends person{
protected String sno;
public student() {
super();
// TODO Auto-generated constructor stub
}
public student(String name, String sex, int age) {
super(name, sex, age);
// TODO Auto-generated constructor stub
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public void play(){
System.out.println("!");
}
}
怎么样?是这个吗?
public class person {
protected String name;
protected String sex;
protected int age;
public person() {
super();
// TODO Auto-generated constructor stub
}
public person(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.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 void showName(){
System.out.println(this.name+"\t");
}
}
package com.xtgj.p1;
public class student extends person{
protected String sno;
public student() {
super();
// TODO Auto-generated constructor stub
}
public student(String name, String sex, int age) {
super(name, sex, age);
// TODO Auto-generated constructor stub
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public void play(){
System.out.println("!");
}
}
怎么样?是这个吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一题:
public class person{
protected String name;
protected String sex;
protected int age;
person(){
name="张三";
sex="男";
age=11;
}
public String toString()
{
return name+sex+age;
}
public static void main(String[] args)
{
person p=new person();
p.toString();
System.out.println(p);
}
}
第二题:
public class Student extends person {
protected int sn;
Student(){
sn=01;
}
public String toString()
{
return "学号"+sn;}
public static void main(String[] args)
{
Student s=new Student();
s.toString();
System.out.println(s);}
}
第三题:
public class banji {
public static void main(String[] args)
{
person p=new person();
Student s=new Student();
System.out.println("教师:"+p);
System.out.println("学生:"+s);
}
}
第四题:我初学 这题还不会
public class person{
protected String name;
protected String sex;
protected int age;
person(){
name="张三";
sex="男";
age=11;
}
public String toString()
{
return name+sex+age;
}
public static void main(String[] args)
{
person p=new person();
p.toString();
System.out.println(p);
}
}
第二题:
public class Student extends person {
protected int sn;
Student(){
sn=01;
}
public String toString()
{
return "学号"+sn;}
public static void main(String[] args)
{
Student s=new Student();
s.toString();
System.out.println(s);}
}
第三题:
public class banji {
public static void main(String[] args)
{
person p=new person();
Student s=new Student();
System.out.println("教师:"+p);
System.out.println("学生:"+s);
}
}
第四题:我初学 这题还不会
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询