两道java程序题 新手请教要怎么写

两道java程序题 新手请教要怎么写,第1张

import javamathBigDecimal;

public class JavaCourse {

private String sName; //学生姓名

private String sId; //学号

private int testGrade; //测试成绩

private int regularGrade; //平时成绩

private int endOfTermGrade;//期末成绩

private int totalGrade; //总成绩

public final double TR= 03;

public final double RR= 03;

public final double ER= 04;

public JavaCourse() {

}

public JavaCourse(String sName, String sId, int testGrade, int regularGrade, int endOfTermGrade) {

thissName = sName;

thissId = sId;

thistestGrade = testGrade;

thisregularGrade = regularGrade;

thisendOfTermGrade = endOfTermGrade;

}

public String getsName() {

return sName;

}

public void setsName(String sName) {

thissName = sName;

}

public String getsId() {

return sId;

}

public void setsId(String sId) {

thissId = sId;

}

public int getTestGrade() {

return testGrade;

}

public void setTestGrade(int testGrade) {

thistestGrade = testGrade;

}

public int getRegularGrade() {

return regularGrade;

}

public void setRegularGrade(int regularGrade) {

thisregularGrade = regularGrade;

}

public int getEndOfTermGrade() {

return endOfTermGrade;

}

public void setEndOfTermGrade(int endOfTermGrade) {

thisendOfTermGrade = endOfTermGrade;

}

public int getTotalGrade() {

return totalGrade;

}

@Override

public String toString() {

return "JavaCourse{" +

"sName='" + sName + '\'' +

", sId='" + sId + '\'' +

", testGrade=" + testGrade +

", regularGrade=" + regularGrade +

", endOfTermGrade=" + endOfTermGrade +

'}';

}

public void showTotalGrad() {

//double四舍五入(05->1)转int类型

totalGrade = IntegerparseInt(new BigDecimal((testGrade TR + regularGrade RR + endOfTermGrade ER))setScale(0, BigDecimalROUND_HALF_UP)toString());

Systemoutprintln("课程的总成绩:" + totalGrade);

}

}

public class DemoTest02 {

public static void main(String[] args) {

JavaCourse student1 = new JavaCourse();

student1setsName("小张");

student1setsId("10086");

student1setTestGrade(85);

student1setRegularGrade(90);

student1setEndOfTermGrade(86);

Systemoutprintln(student1toString());

student1showTotalGrad();

JavaCourse student2 = new JavaCourse("老王","10010",60,80,45);

Systemoutprintln(student2toString());

student2showTotalGrad();

}

}

public interface ShapeArea(){

public double area();

}

public class MyRectangle(){

public static double area(double x,double y){

return xy;

}

public class MyTriangle(){

public static double area(double x,double y,double z){

double s=x+y+z;

returnMathsqrt(s(s-x)(s-y)(s-z));

}

public class Test(){

public static void mian(String[] args){

Systemoutprintln(MyRectanglearea(5,10));

Systemoutprintln(MyTrianglearea(3,4,5));

}

}

}

shape接口

圆类

矩形类

三角形类1

三角形类2

测试类

完整代码如下:

public class Complex {

private float real; //实部

private float imagin;//虚部

public Complex(){//无参默认为(0, 2)

thisreal = 0F;

thisimagin = 2F;

}

public String toString(){//以a+bi的形式输出的复数

return real + "+" + imagin + "i";

}

// a+ bi + (c+ di) = (a+c) + (b+d)i

public static Complex add(Complex c1, Complex c2){//两个复数相加

Complex complex = new Complex();

complexsetReal(c1getReal() + c2getReal());

complexsetImagin(c1getImagin() + c2getImagin());

return complex;

}

// a+ bi == c + di----> a==c, b==d --> it's true

public static boolean equal(Complex c1, Complex c2){//比较两个复数是否相等

return c1getReal() == c2getReal() && c1getImagin() == c2getImagin();

}

//a + bi + f = (a+f) + bi

public static Complex addFloat(Complex c1, float fValue){//复数加一浮点数

c1setReal(c1getReal() + fValue);

return c1;

}

public float getImagin() {

return imagin;

}

public void setImagin(float imagin) {

thisimagin = imagin;

}

public float getReal() {

return real;

}

public void setReal(float real) {

thisreal = real;

}

}

3, 文件名:Threejava

public class Three {

public static void main(String[] args) {

Student stu = new Student("Zhang San", true, (short)12);

Systemoutprintln("Student name: " + stuname);

Systemoutprintln("Student is a male: " + stusex);

Systemoutprintln("Student's age: " + stuage);

stuwork();

stustudy();

Teacher teacher = new Teacher();

teacherlearnMoney();

}

}

abstract class Person{//抽象类Person

protected String name;

protected boolean sex;

protected short age;

protected abstract void work(); //work抽象方法

}

interface Learnmoney{//Learnmoney接口

public void learnMoney();

}

interface Study{//Study接口

public void study();

}

class Student extends Person implements Study{//Student类

public void work() {

Systemoutprintln("学生的工作是努力学习");

}

public Student(String name, boolean sex, short age){

supername = name;

supersex = sex;

superage = age;

}

public void study() {

Systemoutprintln("学生正在学习");

}

}

class Teacher extends Person implements Learnmoney{

public void work() {

Systemoutprintln("教师的工作是教书育人");;

}

public void learnMoney() {

Systemoutprintln("教师正在赚钱");

}

}

class Docotor extends Person implements Learnmoney{

public void work() {

Systemoutprintln("医生的职责是救死扶伤");

}

public void learnMoney() {

Systemoutprintln("医生正在赚钱");

}

}

------------------------------------

4文件名:Fourjava

public class Four {

public static void main(String[] args) {

Rectangle r = new Rectangle(3, 4);

Systemoutprintln("Area is : " + rarea());

Systemoutprintln("Circle is: " + rcircle());

}

}

class Rectangle{

private double width;

private double height;

public Rectangle(double width, double height){

thiswidth = width;

thisheight = height;

}

public double circle(){//求周长

return (width + height) 2;

}

public double area(){//求面积

return width height;

}

}

--------------------

5Fivejava

public class Five {

public static void main(String[] args) {

AImpl a = new AImpl();

apaint();

}

}

interface A {

public int method1(int x);

public int method2(int x, int y);

}

class AImpl implements A{

public int method1(int x) {

return (int)Mathpow(x, 5);

}

public int method2(int x, int y) {

return x > y x: y;

}

public void paint(){

int result1 = method1(2);

int result2 = method2(2, 8);

Systemoutprintln("method1(2) = " + result1);

Systemoutprintln("method2(2, 8) = " + result2);

}

}

-----------------------------测试

method1(2) = 32

method2(2, 8) = 8

楼主,给你给建议,如果,是很急的问题,建议把分标高点(80~120比较适合你的问题),5个编程题,却只给20分,就是全做了,你也只加50分。。这样做很难在你希望的时间内解决问题的,也就是我这种菜鸟会在这里帮你敲代码。。不是别人不帮你,只是这种分的问题,没有多少高手注视,而像我这种注视的,却不能完全解决。

你说朋友让你帮他做两个题,可以发出来就是五个。。。

画图题我不太会,先做了两个,明天再给你把第二题补上,然后我研究下画图题,看能不能帮你解决。。

第二题:

import javaawt;

import javaawtevent;

import javaxswingJTextArea;

public class L {

public static void main(String[] args) {

new C2();

}

}

class C2 extends Frame {

JTextArea t1, t2;

Font f1, f2;

C2() {

f1 = new Font("黑体", FontPLAIN, 24);

f2 = new Font("宋体", FontBOLD + FontITALIC, 16);

t1 = new JTextArea();

t2 = new JTextArea();

t1setFont(f1);

t1setText("张三");

t2setFont(f2);

t2setText("091班 091109");

add(t1, BorderLayoutNORTH);

add(t2, BorderLayoutCENTER);

setLayout(new FlowLayout());

setBounds(100, 100, 180, 120);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

Systemexit(0);

}

});

setVisible(true);

validate();

}

}

第三题:

import javaawt;

import javaawtevent;

public class L {

public static void main(String[] args) {

new C3();

}

}

class C3 extends Frame implements ActionListener {

Label l;

TextField tf;

TextArea ta;

Button b, b1;

C3() {

setLayout(new FlowLayout());

l = new Label("请输入姓名: ");

lsetBackground(Colorcyan);

tf = new TextField(20);

ta = new TextArea(5, 20);

b = new Button(" 确定 ");

baddActionListener(this);

b1 = new Button(" 取消 ");

b1addActionListener(this);

add(l);

add(tf);

add(b);

add(b1);

add(ta);

setBounds(100, 100, 180, 225);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

Systemexit(0);

}

});

setVisible(true);

validate();

}

public void actionPerformed(ActionEvent e) {

if (egetSource() == b) {

String str = "您好," + tfgetText() + ",欢迎访问\n";

taappend(str);

} else if (egetSource() == b1) {

tasetText(null);

}

}

}

第五题:

import javaawt;

import javaawtevent;

import javaxswingJOptionPane;

public class L {

public static void main(String[] args) {

new C5();

}

}

class C5 extends Frame implements ItemListener {

Label l;

CheckboxGroup choice;

Checkbox c1, c2, c3, c4;

C5() {

setLayout(new FlowLayout());

l = new Label("拉丁文第一个字母是: ");

lsetBackground(Colorcyan);

choice = new CheckboxGroup();

c1 = new Checkbox("A", false, choice);

c2 = new Checkbox("B", false, choice);

c3 = new Checkbox("C", false, choice);

c4 = new Checkbox("D", false, choice);

c1addItemListener(this);

c2addItemListener(this);

c3addItemListener(this);

c4addItemListener(this);

add(l);

add(c1);

add(c2);

add(c3);

add(c4);

setBounds(100, 100, 180, 120);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

Systemexit(0);

}

});

setVisible(true);

validate();

}

public void itemStateChanged(ItemEvent e) {

Checkbox b = (Checkbox) egetSource();

if (bgetLabel() == "A") {

JOptionPaneshowMessageDialog(this, "选择正确");

}else

JOptionPaneshowMessageDialog(this, "选择错误,答案选A");

}

}

画图的真不怎么会。。抱歉啊。。

代码如下

class Box{

private int width;

private int length;

private int height;

public Box(int width,int length,int height){

thiswidth = width;

thislength = length;

thisheight = height;

}

public void showBox(){

Systemoutprintln("盒子的width、length、height分别为"+width+","+length+","+height);

}

}

如果有帮助到你,请点击采纳

以上就是关于两道java程序题 新手请教要怎么写全部的内容,包括:两道java程序题 新手请教要怎么写、java语言程序设计题、java程序设计题,求解答等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9272874.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存