Java程序面向对象编程的基本架构 接口的定义和使用 求完整代码……谢谢

Java程序面向对象编程的基本架构 接口的定义和使用 求完整代码……谢谢,第1张

public class Admins implements Userinterface{

private String name;

private String password;

private static int userNum;

public Admins() {

userNum ++;

}

public Admins(String name, String password) {

thisname = name;

thispassword = password;

userNum ++;

}

public void setPassword(String password) {

thispassword = password;

}

public String getPassword() {

return thispassword;

}

public String toString() {

return "Total: " + AdminsuserNum + "\nName: " + thisname + "\nPassword: " + thispassword;

}

public static void main(String [] args) {

Userinterface [] users = new Userinterface[]{new Admins("jeryy", "11111"), new Admins("green", "123123")};

for(Userinterface u: users) {

Systemoutprintln(u);

}

}

}

interface Userinterface{

public void setPassword(String password);

public String getPassword();

public String toString();

}

public class Bins{

public static void main(String [] args) {

int len = 10;

int [] dist = new int [len];

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

dist[i] = (int)(Mathrandom() 100);

}

javautilArrayssort(dist);

Systemoutprintln("生成数组如下: ");

for(int i: dist) {

Systemoutprint(i + "\t");

}

javautilScanner san = new javautilScanner(Systemin);

Systemoutprint("输入要查找的数: ");

int key = sannextInt();

int res = BinsbinSearch(dist, key);

//Systemoutprintln(res);

String info = (res >=0 ) (key + ":" + res) : "查找失败";

Systemoutprintln(info);

}

public static int binSearch(int [] dist, int key) {

javautilArrayssort(dist);

return javautilArraysbinarySearch(dist, key);

}

}

public class Test {

    public static void main(String[] args) {

        // 定义数组

        int[][] array = new int[5][6];

        // 生成随机数初始化数组

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

            for (int j = 0; j < array[i]length; j++) {

                array[i][j] = (int) (40  Mathrandom()) + 10;

            }

        }

        // 输出数组

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

            for (int j = 0; j < array[i]length; j++) {

                Systemoutprint(array[i][j] + "\t");

            }

            Systemoutprintln(); // 换行

        }

    }

}

您好,写了一个程序,求素数,并将所有素数存到ArrayList sushu中:

import javautilArrayList;

import javautilzipInflater;

public class sushu {

public static void main(String[] args) {

int n=50,b=0;

float a=0,c=0;

ArrayList sushu=new ArrayList();

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

int state=0;

for(int j=2;j<(i/2+1);j++){

a=(float)i/(float)j;

//Systemoutprintln(a);

b=(int)a;

//Systemoutprintln(a-b);

c=a-b;

//Systemoutprintln(c);

if(c==0){state=1;break;}

}

if(state==0)sushuadd(i);

}

Systemoutprintln(sushu);

}

}

输出结果为:[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

package comdemo;

import javaawtBorderLayout;

import javaawtDimension;

import javaawteventActionEvent;

import javaawteventActionListener;

import javautilTimer;

import javautilTimerTask;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJTextField;

public class TimerTest {

private Timer timer;

private JTextField field;

private JButton button;

private boolean flag = true;

public TimerTest() {

timer = new Timer();

addview();

}

private void addview() {

JFrame frame = new JFrame("Timer test");

field = new JTextField();

fieldsetPreferredSize(new Dimension(0, 30));

button = new JButton("start");

buttonsetPreferredSize(new Dimension(100, 30));

buttonaddActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (flag) {

auto();

flag = false;

buttonsetText("stop");

} else {

timercancel();

flag = true;

buttonsetText("start");

}

}

});

frameadd(field, BorderLayoutCENTER);

frameadd(button, BorderLayoutEAST);

framesetBounds(200, 200, 300, 60);

framesetVisible(true);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

}

public static void main(String[] args) {

new TimerTest();

}

private void auto() {

timerschedule(new TimerTask() {

@Override

public void run() {

int num = IntegerparseInt(fieldgetText()trim());

num += 1;

fieldsetText(num + "");

auto();

}

}, 1000);

}

}

给你个做好了的Java的源程序的记事本,自己看看就行了的,不怎么难的···

import javaawt;

import javaawtevent;

import javaio;

import javaxswing;

public class MyNotepad implements ActionListener{

private JFrame frame=new JFrame("新记事本");

private JTextArea jta=new JTextArea();

private String result="";

private boolean flag=true;

private File f;

private JButton jb=new JButton("开始");

private JTextField jtf=new JTextField(15);

private JTextField jt=new JTextField(15);

private JButton jbt=new JButton("替换为");

private JButton jba=new JButton("全部替换");

private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11gif");

private String value;

private int start=0;

private JFrame jf=new JFrame("查找");

private JFrame jfc=new JFrame("替换");

@Override

public void actionPerformed(ActionEvent e) {

String comm=egetActionCommand();

if("新建"equals(comm)){

if(!(framegetTitle()equals("新记事本"))){

if(!flag){

write();

newNew();

}else{

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfcshowDialog(null,"保存为");

if(returnVal == JFileChooserAPPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfcgetSelectedFile();

flag=false;

write();

}

}

}else if(!(jtagetText()isEmpty())){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfcshowDialog(null,"保存为");

if(returnVal == JFileChooserAPPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfcgetSelectedFile();

flag=false;

write();

newNew();

}

}else{

newNew();

}

}else if("打开"equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

jfcsetDialogType(JFileChooserOPEN_DIALOG);

int returnVal = jfcshowOpenDialog(null);

if(returnVal == JFileChooserAPPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfcgetSelectedFile();

framesetTitle(fgetName());

result=read();

flag=false;

value=result;

jtasetText(result);

}

}else if("保存"equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

if(flag){

int returnVal = jfcshowDialog(null,"保存为");

if(returnVal == JFileChooserAPPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfcgetSelectedFile();

flag=false;

write();

}

}else{

write();

}

}else if("另存"equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfcshowDialog(null,"另存");

if(returnVal == JFileChooserAPPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfcgetSelectedFile();

write();

}

}else if("退出"equals(comm)){

Systemexit(0);

}else if("撤销"equals(comm)){

jtasetText(value);

}else if("剪切"equals(comm)){

value=jtagetText();

jtacut();

}else if("复制"equals(comm)){

jtacopy();

}else if("粘贴"equals(comm)){

value=jtagetText();

jtapaste();

}else if("删除"equals(comm)){

value=jtagetText();

jtareplaceSelection(null);

}else if("全选"equals(comm)){

jtaselectAll();

}else if("查找"equals(comm)){

value=jtagetText();

jfadd(jtf,BorderLayoutCENTER);

jfadd(jb,BorderLayoutSOUTH);

jfsetLocation(300,300);

jfpack();

jfsetVisible(true);

jfsetDefaultCloseOperation(JFrameDISPOSE_ON_CLOSE);

}else if("替换"equals(comm)){

value=jtagetText();

GridLayout gl=new GridLayout(3,3);

JLabel jl1=new JLabel("查找内容:");

JLabel jl2=new JLabel("替换为:");

jfcsetLayout(gl);

jfcadd(jl1);

jfcadd(jtf);

jfcadd(jb);

jfcadd(jl2);

jfcadd(jt);

jfcadd(jbt);

JLabel jl3=new JLabel();

JLabel jl4=new JLabel();

jfcadd(jl3);

jfcadd(jl4);

jfcadd(jba);

jfcsetLocation(300,300);

jfcpack();

jfcsetVisible(true);

jfcsetDefaultCloseOperation(JFrameDISPOSE_ON_CLOSE);

}else if("版本"equals(comm)){

JDialog jd=new JDialog(frame,"关于对话框");

jdsetSize(200,200);

JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");

jdadd(l,BorderLayoutCENTER);

jdsetLocation(100,200);

jdsetSize(300,300);

jdsetVisible(true);

// jdpack();

jdsetDefaultCloseOperation(JDialogDISPOSE_ON_CLOSE);

}else if("开始"equals(comm)||"下一个"equals(comm)){

String temp=jtfgetText();

int s=valueindexOf(temp,start);

if(valueindexOf(temp,start)!=-1){

jtasetSelectionStart(s);

jtasetSelectionEnd(s+templength());

jtasetSelectedTextColor(ColorGREEN);

start=s+1;

jbsetText("下一个");

// value=valuesubstring(s+templength());//不能截取字串

}else {

JOptionPaneshowMessageDialog(jf, "查找完毕!", "提示", 0, ic);

jfdispose();

}

}else if("替换为"equals(comm)){

String temp=jtfgetText();

int s=valueindexOf(temp,start);

if(valueindexOf(temp,start)!=-1){

jtasetSelectionStart(s);

jtasetSelectionEnd(s+templength());

jtasetSelectedTextColor(ColorGREEN);

start=s+1;

jtareplaceSelection(jtgetText());

}else {

JOptionPaneshowMessageDialog(jf, "查找完毕!", "提示", 0, ic);

jfdispose();

}

}else if("全部替换"equals(comm)){

String temp=jtagetText();

temp=tempreplaceAll(jtfgetText(), jtgetText());

jtasetText(temp);

}

}

public String read(){

String temp="";

try {

FileInputStream fis = new FileInputStream(fgetAbsolutePath());

byte[] b=new byte[1024];

while(true){

int num=fisread(b);

if(num==-1)break;

temp=temp+new String(b,0,num);

}

fisclose();

} catch (Exception e1) {

e1printStackTrace();

}

return temp;

}

public void write(){

try {

FileOutputStream fos=new FileOutputStream(f);

foswrite(jtagetText()getBytes());

fosclose();

} catch (Exception e) {

eprintStackTrace();

}

}

public void newNew(){

framedispose();

new MyNotepad();

flag=true;

}

public MyNotepad(){

JMenuBar jmb=new JMenuBar();

String[] menuLab={"文件","编辑","帮助"};

String[][] menuItemLab={{"新建","打开","保存","另存","退出"},

{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},

{"版本"}};

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

JMenu menu=new JMenu(menuLab[i]);

jmbadd(menu);

for(int j=0;j<menuItemLab[i]length;j++){

JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);

menuadd(jmi);

jmiaddActionListener(this);

}

}

framesetJMenuBar(jmb);

jtasetLineWrap(true);//自动换行

JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板

frameadd(jsp);

jbaddActionListener(this);

jbtaddActionListener(this);

jbaaddActionListener(this);

framesetLocation(200,50);

framesetSize(620,660);

framesetVisible(true);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

}

public static void main(String[] args) {

new MyNotepad();

}

}

以上就是关于Java程序面向对象编程的基本架构 接口的定义和使用 求完整代码……谢谢全部的内容,包括:Java程序面向对象编程的基本架构 接口的定义和使用 求完整代码……谢谢、java编程(写出程序代码)、JAVA源程序代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存