JAVA入门编程。。很简单。希望是原创的越短越简单越好,写的好有追加分!!
发布网友
发布时间:2022-04-23 21:26
我来回答
共4个回答
热心网友
时间:2023-10-10 03:23
第一题:
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";
}
}
热心网友
时间:2023-10-10 03:23
Use.java
----------
class Use {
public static void main(String[] args){
Class c = new Class("摄像班");
System.out.println(c);
}
}
class People {
String name;
String sex;
String age;
public People(String name,String sex,String age){
this.name = name;
this.sex = sex;
this.age = age;
}
public String toString(){
String str = "name is "+name+",sex is "+sex+",age is "+age;
return str;
}
}
class Student extends People{
String sn;
public Student(String name, String sex, String age,String sn) {
super(name, sex, age);
this.sn = sn;
}
public String toString(){
String str = "name is "+name+",sex="+sex+",age="+age+" ,sn="+sn;
return str;
}
}
class Class {
People teacher ;
Student student;
String className;
public Class(String className){
teacher = new People("陈老师","男","38");
student = new Student("苍jk","女","18","419");
this.className = className;
}
public String toString(){
String str = "this className is"+className+" ,the teacher "+teacher+",the student "+student;
return str;
}
}
---------------------------------------------------------
运行结果:
this className is摄像班 ,the teacher name is 陈老师,sex is 男,age is 38,the student name is 苍jk,sex=女,age=18 ,sn=419
------------------------------------------------------
先写一个
热心网友
时间:2023-10-10 03:24
计算器传送门
http://wenku.baidu.com/view/02e0d91a964bcf84b9d57baa.html
热心网友
时间:2023-10-10 03:24
是想用awt包还是swing包写。追问都可以。。。谢谢了。。哪个简单用哪个把。。