java编程doc.docx
《java编程doc.docx》由会员分享,可在线阅读,更多相关《java编程doc.docx(20页珍藏版)》请在冰豆网上搜索。
java编程doc
四.编程题
1、定义一个长方形类,该类能实现求长方形的面积,长方形的周长。
publicclassChang{
doublex,y;
publicChang(doublex,doubley){
this.x=x;
this.y=y;
}
publicdoublegetArea(){//计算面积
returnx*y;
}
publicdoublegetZC(){//计算周长
return2*(x+y);
}
publicstaticvoidmain(String[]args){
}
}
2、定义一个圆类,该类能实现求圆的面积,圆的周长。
importjava.util.Scanner;
classRound{
publicdoubleperimeter(doubleradius){
returnradius*2;
}
publicdoublearea(doubleradius){
returnradius*radius*3.14;
}
publicvoidprint(doubleradius){
System.out.println("圆的周长为-->"+this.perimeter(radius)+"\n"+
"圆的面积为-->"+this.area(radius));
}
}
publicclassOperate{
publicstaticvoidmain(Stringargs[]){
Roundround=newRound();
System.out.print("请输入圆的半径-->");
Scannerscan=newScanner(System.in);
doubleradius=scan.nextDouble();
round.print(radius);
}
}
结果:
请输入圆的半径-->3
圆的周长为-->6
圆的面积为-->28.26
3、设计一个盒子类,该类能实现求盒子的表面积和体积。
publicclassCube{
privatefloatwidth;
privatefloatlength;
privatefloatheight;
privatefloatvolume;
privatefloatarea;
publicCube(){
super();
//TODOAuto-generatedconstructorstub
}
publicCube(floatwidth,floatlength,floatheight){
super();
this.width=width;
this.length=length;
this.height=height;
}
publicfloatgetWidth(){
returnwidth;
}
publicvoidsetWidth(floatwidth){
this.width=width;
}
publicfloatgetLength(){
returnlength;
}
publicvoidsetLength(floatlength){
this.length=length;
}
publicfloatgetHeight(){
returnheight;
}
publicvoidsetHeight(floatheight){
this.height=height;
}
publicfloatgetVolume(){
returnvolume;
}
publicvoidsetVolume(floatvolume){
this.volume=volume;
}
publicfloatgetArea(){
returnarea;
}
publicvoidsetArea(floatarea){
this.area=area;
}
@Override
publicStringtoString(){
return"Cube[width="+width+",lenght="+length+",height="
+height+"]";
}
//体积计算
publicvoidcalVolume(){
volume=length*height*width;
}
//表面积计算
publicvoidcalArea(){
area=2*((length*width)+(length*height)+(width*height));
}
publicstaticvoidmain(String[]args){
Cubecube=newCube(20,30,40);
//数据长宽高
System.out.println(cube);
cube.calVolume();
cube.calArea();
System.out.println("体积是:
"+cube.getVolume());
System.out.println("表面积是:
"+cube.getArea());
}
}
结果:
Cube【width=20.0,length=30.0,height=40.0】
体积是:
24000.0
表面积是:
5200.0
4、设计一个球类。
能求简易数列的结果
A、10!
publicclassqiu1{
publicstaticvoidmain(String[]args){
longsum=0;
longfac=1;
for(inti=1;i<=10;i++){
fac=fac*i;
sum+=fac;
}
System.out.println(sum);
}
}
结果:
4037913
B、1-1/2+1/3-1/4+…….-1/100
classqiu2{
publicstaticvoidmain(Stringargs[])
{
inti;
intflag=1;
doublesum=0;
for(i=1;i<=100;i++)
{
sum=sum+flag*1.0/i;
flag=-flag;
}
System.out.println(sum);
}
}
结果:
0.688172179310195
C、求Fabonacci数列前20项
publicclassFibonacci{
publicstaticvoidmain(String[]args){
intn0=1,n1=1,n2;
System.out.print(n0+""+n1+"");
for(inti=0;i<18;i++){
n2=n1+n0;
System.out.print(n2+"");
n0=n1;
n1=n2;
}
System.out.println();
}
结果:
11235813213455891442333776109871597258441816765
D、编程求1到100之间能被3整除的整数和
publicclasschu3{
publicstaticvoidmain(String[]args){
chu3s=newchu3();
System.out.println(s.sumTotal());
}
publicintsumTotal(){
inttotal=0;
for(inti=0;i<=100;i++){
if(i%3==0){
total=total+i;
}
}
returntotal;
}
}
结果:
1683
能制作简易图形界面
4.登入界面
packageshiyan7;
importjava.awt.*;
importjavax.swing.*;
publicclassLogin2extendsJFrame{
JLabell1,l2,l3;
JButtonlogin,exit;
JTextFieldusername,passthword;
publicLogin2(Strings){
super(s);
JLabell1=newJLabel("欢迎登入");
JLabell2=newJLabel("用户名:
");
JLabell3=newJLabel("密码:
");
JButtonlogin=newJButton("登入");
JButtonexit=newJButton("退出");
username=newJTextField();
passthword=newJTextField();
l1.setFont(newFont("宋体",0,26));
l1.setForeground(Color.blue);
l2.setFont(newFont("宋体",0,15));
l3.setFont(newFont("宋体",0,15));
login.setFont(newFont("宋体",0,15));
exit.setFont(newFont("宋体",0,15));
l1.setBounds(100,20,250,50);
l2.setBounds(70,80,80,30);
l3.setBounds(70,120,80,30);
username.setBounds(160,80,120,30);
passthword.setBounds(160,120,120,30);
login.setBounds(100,200,80,30);
exit.setBounds(200,200,80,30);
add(l1);
add(l2);
add(l3);
add(login);add(exit);
add(username);add(passthword);
setBounds(100,100,400,300);
setLayout(null);
setVisible(true);
}
publicstaticvoidmain(String[]args){
newLogin2("欢迎登入");
}
}
6、常见系统的主界面设计,如学生管理系统(设计菜单项、工具栏、简易时间处理)
packageiframe;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.border.BevelBorder;
publicclassLibraryextendsJFrame{
privatestaticfinalJDesktopPaneDESKTOP_PANE=newJDesktopPane();
publicstaticvoidmain(String[]args){
try{
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
newLogin3();//登录窗口
}catch(Exceptionex){
ex.printStackTrace();
}
}
publicstaticvoidaddIFame(JInternalFrameiframe){//添加子窗体的方法
DESKTOP_PANE.add(iframe);
}
publicLibrary(){
super();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationByPlatform(true);//设置在下次窗口可见时,此窗口是应显示在本机窗口系统的默认位置,还是应显示在当前位置(由getLocation返回)。
setSize(800,600);
setTitle("图书馆管理系统");
JMenuBarmenuBar=createMenu();//调用创建菜单栏的方法
setJMenuBar(menuBar);
JToolBartoolBar=createToolBar();//调用创建工具栏的方法
getContentPane().add(toolBar,BorderLayout.NORTH);
finalJLabellabel=newJLabel();
label.setBounds(0,0,0,0);
label.setIcon(null);//窗体背景
DESKTOP_PANE.addComponentListener(newComponentAdapter(){
publicvoidcomponentResized(finalComponentEvente){
Dimensionsize=e.getComponent().getSize();
label.setSize(e.getComponent().getSize());
label.setText("+size.height+"src='"
+this.getClass().getResource("backImg.jpg")
+"'>");
}
});
DESKTOP_PANE.add(label,newInteger(Integer.MIN_VALUE));
getContentPane().add(DESKTOP_PANE);
}
privateJToolBarcreateToolBar(){//创建工具栏的方法
JToolBartoolBar=newJToolBar();
toolBar.setFloatable(false);
toolBar.setBorder(newBevelBorder(BevelBorder.RAISED));//突起
JButtonExitButton=newJButton();
ImageIconExiticon=newImageIcon("res/exittb.jpg");//CreatecdIcon.add("exittb.jpg");//创建图标方法
ExitButton.setIcon(Exiticon);
ExitButton.setHideActionText(true);
ExitButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.exit(0);
}
});
toolBar.add(ExitButton);
returntoolBar;
}
privateJMenuBarcreateMenu(){//创建菜单栏的方法
JMenuBarmenuBar=newJMenuBar();
JMenubaseMenu=newJMenu("基础数据维护");
{
JMenubaseMenu1=newJMenu("图书信息管理");
JMenuItembaseMenu11=newJMenuItem("图书信息添加");
JMenuItembaseMenu12=newJMenuItem("图书信息修改");
JMenuItembaseMenu2=newJMenuItem("退出系统");
baseMenu11.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
BookAddIFramebaIFrame=newBookAddIFrame();
DESKTOP_PANE.add(baIFrame);
baIFrame.toFront();
baIFrame.show();
}
});
baseMenu2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.exit(0);
}
});
baseMenu1.add(baseMenu11);
baseMenu1.add(baseMenu12);
baseMenu.add(baseMenu1);
baseMenu.addSeparator();
baseMenu.add(baseMenu2);
}
JMenusysManageMenu=newJMenu();//系统维护
sysManageMenu.setIcon(newImageIcon("res/jcwhcd.jpg"));//CreatecdIcon.add("jcwhcd.jpg"));
JMenuuserManageMenu=newJMenu("用户管理");//用户管理
JMenuItemuserManageMenu1=newJMenuItem("用户添加");
JMenuItemuserManageMenu2=newJMenuItem("用户修改与删除");
userManageMenu1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
}
});
userManageMenu2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
}
});
userManageMenu.add(userManageMenu1);
userManageMenu.add(userManageMenu2);
sysManageMenu.add(userManageMenu);
menuBar.add(baseMenu);//添加基础数据维护菜单到菜单栏
menuBar.add(sysManageMenu);//添加系统维护菜单到菜单栏
returnmenuBar;
}
}
7、输入两个数求两数之和、差、积,商
packageiframe;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JTextField;
importjava.applet.*;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.*;
publicclassE5{
JFramef=newJFrame("简单的计算器");
JLabelnum1=newJLabel("Number1");
JLabelnum2=newJLabel("Number2");
JTextFieldinput1=newJTextField();
JTextFieldinput2=newJTextField();
JLabel和=newJLabel("和");
JTextFieldtext1=newJTextField(10);
JLabel差=newJLabel("差");
JTextFieldtext2=newJTextField(10);
JLabel积=newJLabel("积");
JTextFieldtext3=newJTextField(10);
JLabel商=newJLabel("商");
privateJTextFieldtext4=newJTextField(10);
publicE5(){
text1.setEditable(false);
text2.setEditable(false);
text3.setEditable(false);
text4.setEditable(false);
input1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(input2.getText()==null||input2.getText().trim().equals("")){
return;
}
if(input1.getText()!
=null&&input2.getText()!
=null){
doublenumber1=0d,number2=0d;
try{