java 程序设计题

java 程序设计题,第1张

//本来不打算回答的,要写这么多,分又这么低,不厚道啊,后来想想反正java我也是刚学,就当做练手吧。:-)

//编写一个Application程序,比较命令中给出的两个字符串是否相等,并输出比较的结果

public class StringMatch

{

public static void main(String args[])

{

Systemoutprintln(args[0]equals(args[1]));

}

}

//设计一:表示图书的book类:包括书名,作者,月销售量等属性,其中有两个构造方法:一个无参,

//一个有参,两个成员方法:setbook用于设置书名,作者月销售量等属性display用于输出书名。

class book

{

String name;

String writer;

String saleCount;

public book()

{}

public book(String name,String writer,String saleCount)

{

thisname=name;

thiswriter=writer;

thissaleCount=saleCount;

}

public void display()

{

Systemoutprintln("书名:"+name);

}

}

//设计二employer类:姓名,地址,省市,邮编等属性,并在构造方法中初始化属性,两个成员方法:

//changename用于改名字,display用于把对象完整打印,属性是受保护的,方法是公共的。

class employer

{

proceted String name;

proceted String addr;

proceted String province;

proceted String postNum;

public employer()

{}

public employer(String name,String addr,String province,String postNum)

{

thisname=name;

thisaddr=addr;

thisprovince=province;

thispostNum=postNum;

}

public void display()

{

Systemoutprintln("姓名:"+name+"地址:"+addr);

Systemoutprintln("省市:"+province+"邮编:"+postNum);

}

}

//设计三:以员工类为父类,经理,工人均继承该类,创建经理类,工人类,输出经理,工人的工资。

//在父类中编写一个成员方法Getsalary,并在子类中实现覆盖,实现多态。

class Stuff

{

protected int salary;

public int GetSalary()

{

return salary;

}

public void ShowSalary()

{

Systemoutprintln("员工工资为:"+salary);

}

}

class Manager extends Stuff

{

public void ShowSalary()

{

Systemoutprintln("经理工资为:"+salary);

}

}

class Worker extends Stuff

{

public void ShowSalary()

{

Systemoutprintln("工人工资为:"+salary);

}

}

//设计四:定义一个student类,数据成员:学号,班号,姓名,性别,年龄,

//定义一个成员方法获得学好,班号,姓名,性别,年龄。

//要求为student定义构造方法,初始化所有成员,另外增加构造方法tostring,

//把student类对象的所有信息组合成一个字符串。

class Student

{

String stuID;

String classNum;

String name;

String gender;

int age;

public Student(String stuID,String classNum,String name,String gender,int age)

{

thisstuID=stuID;

thisclassNum=classNum;

thisname=name;

thisgender=gender;

thisage=age;

}

public String[] GetStudnet()

{

String[] s=new String[5];

s[0]=stuID;

s[1]=classNum;

s[2]=name;

s[3]=gender;

s[4]=IntegertoString(age);

return s;

}

public String toString()

{

StringBuffer sb=new StringBuffer();

sbappend("学号:"+stuID);

sbappend("班号:"+classNum);

sbappend("姓名:"+name);

sbappend("性别:"+gender);

sbappend("年龄:"+age);

return sbtoString();

}

}

//设计五:定义一个接口,两个方法Method1,Method2返回值类型均为int型。

//定义一个程序实现接口要求M1的功能求5(X)次方,M2为为求两个数的较大值。

//print方法要求调用M1,M2,求M1(2),M2(2,8)

interface Itest

{

int Method1(double b);

int Method2(double c,double d);

}

class Test implements Itest

{

public int Method1(double b)

{

return (int)Mathpow(5,b);

}

public int Method2(double c,double d)

{

return (int)Mathmax(c,d);

}

void print(double b,double c,double d)

{

Systemoutprintln(Method1(b));

Systemoutprintln(Method2(c,d));

}

}

public class Demon

{

public static void main(String args[])

{

Test d=new Test();

dprint(2,2,8);

}

}

importjavaio;

importjavaioFile;

importjavaioFileReader;

importjavaioFileWriter;

importjavaioIOException;

publicclassNewTxt{

publicstaticvoidmain(String[]args)throwsIOException{

intn=5;//NN数组

double[][]arr=newdouble[n][n];//插入的数组

double[][]arr2=newdouble[n][n];;//读取出的数组

//数组初始化,随机生成的[0,100)之间的double数

for(inti=0;i

for(intj=0;j

arr[i][j]=Mathrandom()100;

Systemoutprintln(arr[i][j]);

}

}

Filefile=newFile("d:\arraytxt");//存放数组数据的文件

FileWriterout=newFileWriter(file);//文件写入流

//将数组中的数据写入到文件中。每行各数据之间TAB间隔

for(inti=0;i

for(intj=0;j

outwrite(arr[i][j]" ");

}

outwrite("");

}

outclose();

in=new(newFileReader(file));//

Stringline;//一行数据

introw=0;

//逐行读取,并将每个数组放入到数组中

while((line=inreadLine())!=null){

String[]temp=linesplit(" ");

for(intj=0;j

arr2[row][j]=DoubleparseDouble(temp[j]);

}

row;

}

inclose();

//显示读取出的数组

for(inti=0;i

for(intj=0;j

Systemoutprint(arr2[i][j]" ");

}

Systemoutprintln();

}

}

}

class Complex{

private int realPart;

private int imaginPart;

Complex(){

thisrealPart = 0;

thisimaginPart = 0;

}

Complex(int realPart, int imaginPart){

thisrealPart = realPart;

thisimaginPart = imaginPart;

}

public void setRealPart(int realPart){

thisrealPart = realPart;

}

public void setImaginPart(int imaginPart){

thisimaginPart = imaginPart;

}

public int getRealPart(){

return thisrealPart;

}

public int getImaginPart(){

return thisimaginPart;

}

Complex complexAdd(Complex a){

int realPart = thisrealPart + agetRealPart();

int imaginPart = thisimaginPart + agetImaginPart();

return new Complex(realPart, imaginPart);

}

public String toString(){

if(thisimaginPart == 0) return "" + thisrealPart;

else if(thisimaginPart > 0) return "" + thisrealPart + "+" + thisimaginPart + "i";

else return "" + thisrealPart + thisimaginPart + "i";

}

}

public class TestComplex{

public static void main(String[] args){

Complex a = new Complex(1,2);

Complex b = new Complex(3,4);

Complex sum = acomplexAdd(b);

Systemoutprintln("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");

asetRealPart(2);

asetImaginPart(3);

bsetRealPart(bgetRealPart() 2);

bsetImaginPart(bgetImaginPart() 2);

sum = acomplexAdd(b);

Systemoutprintln("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");

}

}

直接赋值粘贴

public class Commodity {

private String type;

private String noun;

private double salePrice;

private String unit;

private String size;

private double percent;

public Commodity(String type, String noun, double salePrice, String unit, String size, double percent) {

super();

thistype = type;

thisnoun = noun;

thissalePrice = salePrice;

thisunit = unit;

thissize = size;

thispercent = 1;

}

public double getSalePrice() {

return salePrice;

}

public void setSalePrice(double salePrice) {

thissalePrice = salePrice;

}

public double getPercent() {

return percent;

}

public void setPercent(double percent) {

thispercent = percent;

}

@Override

public String toString() {

return "Commodity [type=" + type + ", noun=" + noun + ", salePrice=" + salePrice + ", unit=" + unit + ", size="

+ size + ", percent=" + percent + "]";

}

}

请采纳,谢谢

import javaawtFlowLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJTextField;

public class A implements ActionListener {

JFrame jf;

JLabel jl1;

JLabel jl2;

JLabel jl3;

JButton jb1;

JTextField jtf1;

JTextField jtf2;

JTextField jtf3;

public A(){

init();

showMe();

}

public void init(){

jf=new JFrame("拆分小数点");

jfsetLayout(new FlowLayout());

jl1=new JLabel("请输入小数");

jl2=new JLabel("整数为");

jl3=new JLabel("小数为");

jb1=new JButton("拆分");

jb1addActionListener(this);

jtf1=new JTextField(10);

jtf2=new JTextField(10);

jtf2setEditable(false);

jtf3=new JTextField(10);

jtf3setEditable(false);

jfadd(jl1);

jfadd(jtf1);

jfadd(jl2);

jfadd(jtf2);

jfadd(jl3);

jfadd(jtf3);

jfadd(jb1);

}

public void showMe(){

jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

jfsetVisible(true);

jfsetLocation(300, 300);

jfpack();

}

@Override

public void actionPerformed(ActionEvent e) {

String comm=egetActionCommand();

if("拆分"equals(comm)){

String s=jtf1getText();

Systemoutprintln(s);

if(scontains("")){

String[] ss=ssplit("\\");

for (int i = 0; i < sslength; i++) {

Systemoutprintln(ss[i]);

}

jtf2setText(ss[0]);

jtf3setText(ss[1]);

}

}

}

public static void main(String[] args) {

new A();

}

}

import javaawtFlowLayout;

import javaawtGridLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javautilArrays;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJPanel;

import javaxswingJTextField;

public class B implements ActionListener {

public JFrame jf;

private JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8,jl9,jl10,jl11,jl12;

private JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6,jtf7,jtf8,jtf9,jtf10,jtf11,jtf12;

private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8,jp9,jp10,jp11,jp12,jp13;

private JButton jb;

public B(){

init();

showMe();

}

public void showMe(){

jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

jfsetVisible(true);

jfsetLocation(300, 300);

jfpack();

}

public void init(){

jl1=new JLabel("输入第1个数字");

jl2=new JLabel("输入第2个数字");

jl3=new JLabel("输入第3个数字");

jl4=new JLabel("输入第4个数字");

jl5=new JLabel("输入第5个数字");

jl6=new JLabel("输入第6个数字");

jl7=new JLabel("输入第7个数字");

jl8=new JLabel("输入第8个数字");

jl9=new JLabel("输入第9个数字");

jl10=new JLabel("输入第10个数字");

jl11=new JLabel("最大值为");

jl12=new JLabel("最小值为");

jl11setEnabled(false);

jl12setEnabled(false);

jtf1=new JTextField(10);

jtf2=new JTextField(10);

jtf3=new JTextField(10);

jtf4=new JTextField(10);

jtf5=new JTextField(10);

jtf6=new JTextField(10);

jtf7=new JTextField(10);

jtf8=new JTextField(10);

jtf9=new JTextField(10);

jtf10=new JTextField(10);

jtf11=new JTextField(10);

jtf11setEditable(false);

jtf12=new JTextField(10);

jtf12setEditable(false);

jp1=new JPanel();

jp2=new JPanel();

jp3=new JPanel();

jp4=new JPanel();

jp5=new JPanel();

jp6=new JPanel();

jp7=new JPanel();

jp8=new JPanel();

jp9=new JPanel();

jp10=new JPanel();

jp11=new JPanel();

jp12=new JPanel();

jp13=new JPanel();

jb=new JButton("确定");

jbaddActionListener(this);

jp1setLayout(new FlowLayout());

jp1add(jl1);

jp1add(jtf1);

jp2setLayout(new FlowLayout());

jp2add(jl2);

jp2add(jtf2);

jp3setLayout(new FlowLayout());

jp3add(jl3);

jp3add(jtf3);

jp4setLayout(new FlowLayout());

jp4add(jl4);

jp4add(jtf4);

jp5setLayout(new FlowLayout());

jp5add(jl5);

jp5add(jtf5);

jp6setLayout(new FlowLayout());

jp6add(jl6);

jp6add(jtf6);

jp7setLayout(new FlowLayout());

jp7add(jl7);

jp7add(jtf7);

jp8setLayout(new FlowLayout());

jp8add(jl8);

jp8add(jtf8);

jp9setLayout(new FlowLayout());

jp9add(jl9);

jp9add(jtf9);

jp10setLayout(new FlowLayout());

jp10add(jl10);

jp10add(jtf10);

jp11setLayout(new FlowLayout());

jp11add(jl11);

jp11add(jtf11);

jp12setLayout(new FlowLayout());

jp12add(jl12);

jp12add(jtf12);

jf=new JFrame("最大值最小值");

jfsetLayout(new GridLayout(13, 1));

jfadd(jp1);

jfadd(jp2);

jfadd(jp3);

jfadd(jp4);

jfadd(jp5);

jfadd(jp6);

jfadd(jp7);

jfadd(jp8);

jfadd(jp9);

jfadd(jp10);

jfadd(jp11);

jfadd(jp12);

jfadd(jb);

}

@Override

public void actionPerformed(ActionEvent e) {

String comm=egetActionCommand();

if("确定"equals(comm)){

int i1=IntegerparseInt(jtf1getText());

int i2=IntegerparseInt(jtf2getText());

int i3=IntegerparseInt(jtf3getText());

int i4=IntegerparseInt(jtf4getText());

int i5=IntegerparseInt(jtf5getText());

int i6=IntegerparseInt(jtf6getText());

int i7=IntegerparseInt(jtf7getText());

int i8=IntegerparseInt(jtf8getText());

int i9=IntegerparseInt(jtf9getText());

int i10=IntegerparseInt(jtf10getText());

int arr[]={i1,i2,i3,i4,i5,i6,i7,i8,i9,i10};

Arrayssort(arr);

int max=arr[9];

int min=arr[0];

jtf11setText(max+"");

jtf12setText(min+"");

}

}

public static void main(String[] args) {

new B();

}

}

import javaawtFlowLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javautilArrays;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJTextField;

public class C implements ActionListener {

private JFrame jf;

private JLabel jl1,jl2;

private JTextField jtf1,jtf2;

JButton jb1;

public C(){

init();

showMe();

}

public void init(){

jf=new JFrame("字母排序");

jfsetLayout(new FlowLayout());

jl1=new JLabel("请输入字母");

jl2=new JLabel("排序后");

jb1=new JButton("排序");

jb1addActionListener(this);

jtf1=new JTextField(10);

jtf2=new JTextField(10);

jtf2setEditable(false);

jfadd(jl1);

jfadd(jtf1);

jfadd(jl2);

jfadd(jtf2);

jfadd(jb1);

}

public void showMe(){

jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

jfsetVisible(true);

jfsetLocation(300, 300);

jfpack();

}

@Override

public void actionPerformed(ActionEvent e) {

String comm=egetActionCommand();

if("排序"equals(comm)){

String s=jtf1getText();

char[] c=new char[slength()];

if(sendsWith("#")){

for (int i = 0; i < slength(); i++) {

c[i]=scharAt(i);

}

}

Arrayssort(c);

String ss="";

for (int i = 0; i < clength; i++) {

ss=ss+c[i];

}

jtf2setText(sssubstring(1));

}

}

public static void main(String[] args) {

new C();

}

}

都是有界面的,写界面不难 就是很麻烦。。。我自己写的 都是可以直接运行的,有什么问题可以再问我。。。

之前有人问过同样的问题,我回答的,你们是不是同一个系的,要做课程设计?我直接粘贴过来:

我自己写了一个简单的程序,可选择落子的先后顺序,重新开始,最后判断某一方是否为五子连珠。选择落子的先后顺序,只需定义一个boolean变量,每次切换取其反值;重制棋盘或重新开始就把棋盘重新绘制一遍;判断某一方是否为五子连珠,就判断某方的每个棋子,以它为中心与之紧邻的水平,垂直,左斜,右斜四个方向是否有五子连珠。用一个二维数组position存储棋盘上的棋子情况,position[x][y]=1,0,-1分别表示棋盘的第x行第y列下有黑子,无子,白子。源代码如下:

package comtest;

import javaawt;

import javautil;

import javaawtgeom;

import javaawtevent;

import javaxswing;

public class MyFiveChess {

public static void main(String[] args) {

JFrame f = new JFrame();

fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();

int screenWidth = screenSizewidth;

int screenHeight = screenSizeheight;

fsetSize(screenWidth / 2, screenHeight / 2);

fsetLocation(screenWidth / 4, screenHeight / 4);

fsetTitle("FiveChess");

MyPanel panel = new MyPanel();

fadd(panel);

fsetVisible(true);

}

}

class MyPanel extends JPanel {

private static final int SIDELENGTH = 10;

private ArrayList<Ellipse2D> squares = new ArrayList<Ellipse2D>();;

private Ellipse2D current = null;

JButton jb = new JButton("重新开始");

JButton jb2 = new JButton("切换先手");

boolean isBlack;

boolean first = true;

boolean isOver;

int l = 16;

int n = 20;

int bx = 20;

int by = 20;

int[][] position = new int[n + 1][n + 1];

public MyPanel(){

jbaddActionListener(new MyActionHandler());

jb2addActionListener(new MyActionHandler());

addMouseListener(new MouseHandler());

addMouseMotionListener(new MouseMotionHandler());

add(jb);

add(jb2);

}

public void initMyPenal(){

squares = new ArrayList<Ellipse2D>();

current = null;

isBlack = first;

isOver = false;

position = new int[n + 1][n + 1];

for(int i = 0; i <= n; i++)

for(int j = 0; j <= n; j++)

position[i][j] = 0;

repaint();

}

public void paint(Graphics g) {

superpaint(g);

jbsetLocation(400,150);

jb2setLocation(400,200);

gsetColor(ColorRED);

gsetFont(new Font(null, FontBOLD, 20));

gdrawString((first "黑" : "白")+"方下子", 400, 100);

gsetColor(new Color(240, 210, 120));

gfillRect(bx - l, by - l, l (n + 2), l (n + 2));

gsetColor(ColorBLACK);

for (int i = 0; i <= n; i++){

gdrawLine(bx, by + i l, bx + l n, by + i l);

gdrawLine(bx + i l, by, bx + i l, by + l n);

}

Graphics2D g2 = (Graphics2D)g;

isBlack = first;

for(Ellipse2D r : squares){

g2setColor(isBlack ColorBLACK : ColorWHITE);

g2fill(r);

isBlack = !isBlack;

}

if(isOver) {

gsetColor(ColorRED);

gsetFont(new Font("TimesRoman", FontBOLD, 60));

gdrawString((isBlack "白" : "黑") + "方获胜", 120, 200);

}

}

public Ellipse2D find(Point2D p){

for(Ellipse2D r : squares)

if(rcontains(p))

return r;

return null;

}

public void add(Point2D p) {

if(pgetX() > bx - l / 2 && pgetX() < bx + l n + l / 2 &&

pgetY() > by - l / 2 && pgetY() < by + l n + l / 2){

for (int i = 0; i <= n; i++) {

for (int j = 0; j <= n; j++) {

if(position[i][j] == 1 || position[i][j] == -1) continue;

current = new Ellipse2DDouble(bx + j l - l / 2,

by + i l - l / 2, l, l);

if (currentcontains(p)) {

position[i][j] = isBlack 1 : -1;

isOver = isWin(position, isBlack, i , j) true : false;

currentsetFrame(bx + j l - l / 2 + 1,

by + i l - l / 2 + 1, l - 2, l - 2);

squaresadd(current);

repaint();

return;

}

}

}

}

}

private class MouseHandler extends MouseAdapter{

public void mousePressed(MouseEvent event){

if(isOver) return;

current = find(eventgetPoint());

if(current == null)

add(eventgetPoint());

}

}

private class MyActionHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {

String cmd=egetActionCommand();

if("重新开始"equals(cmd)){

initMyPenal();

}else if("切换先手"equals(cmd)){

initMyPenal();

first=!first;

}

}

}

private class MouseMotionHandler implements MouseMotionListener{

public void mouseMoved(MouseEvent event){

Rectangle r = new Rectangle(bx - l, by - l, l (n + 2), l (n + 2));

if(rcontains(eventgetPoint())){

setCursor(CursorgetPredefinedCursor(CursorCROSSHAIR_CURSOR));

}else setCursor(CursorgetDefaultCursor());

}

public void mouseDragged(MouseEvent event){}

}

public boolean isWin(int[][] state, boolean isBlack, int x, int y) {//四个方向中是否有五子连珠

return isCzWin(state, isBlack, x, y)

|| isSpWin(state, isBlack, x, y)

|| isYxWin(state, isBlack, x, y)

|| isZxWin(state, isBlack, x, y);

}

public boolean isCzWin(int[][] state, boolean isBlack, int x, int y) {//判断垂直方向是否有五子连珠

int n = 0;

int a = (x >= 4 x - 4 : 0);

int b = (x <= statelength - 5 x + 4 : statelength - 1);

for (int i = a; i <= b; i++)

if (state[i][y] == (isBlack 1: -1)) {

if (++n == 5) return true;

} else n = 0;

return false;

}

public boolean isSpWin(int[][] state, boolean isBlack, int x, int y) {//判断水平方向是否有五子连珠

int n = 0;

int a = (y >= 4 y - 4 : 0);

int b = (y <= state[0]length - 5 y + 4 : state[0]length - 1);

for (int i = a; i <= b; i++)

if (state[x][i] == (isBlack 1: -1)) {

if (++n == 5) return true;

} else n = 0;

return false;

}

public boolean isZxWin(int[][] state, boolean isBlack, int x, int y) {//判断左斜方向是否有五子连珠

int n = 1, a = x, b = y;

for (int i = 1; i <= 4 && a > 0 && b > 0; i++)

if (state[a - 1][b - 1] == (isBlack 1: -1)) {

n++; a--; b--;

} else break;

for (int i = 1; i <= 4 && x < statelength - 1 && y < state[0]length - 1; i++)

if (state[x + 1][y + 1] == (isBlack 1: -1)) {

n++; x++; y++;

} else break;

if (n >= 5) return true;

return false;

}

public boolean isYxWin(int[][] state, boolean isBlack, int x, int y) {//判断右斜方向是否有五子连珠

int n = 1, a = x, b = y;

for (int i = 1; i <= 4 && a > 0 && b < state[0]length - 1; i++)

if (state[a - 1][b + 1] == (isBlack 1: -1)) {

n++; a--; b++;

} else break;

for (int i = 1; i <= 4 && x < statelength - 1 && y > 0; i++)

if (state[x + 1][y - 1] == (isBlack 1: -1)) {

n++; x++; y--;

} else break;

if (n >= 5) return true;

return false;

}

}

比较简略,自己可以根据情况修改,改进改进!

以上就是关于java 程序设计题全部的内容,包括:java 程序设计题、Java程序设计(java程序设计答案)、JAVA程序设计 急要 谢谢~~~等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存