2015年計算機二級考試JAVA考前操作題和簡單應用題

字號:

二、基本操作題(共18分)
    本題中定義了一個長度為20的整數(shù)數(shù)組,然后將1~20分別賦給數(shù)組元素,計算該數(shù)組中所有下標為奇數(shù)的元素的和。
    public class javal{
    public static void main(String args[]){
    int sum;
     ;
    int arrayList[]=new int[20];
    for(int i=0;i<=19;i++)
    arrayList[i]=i+1;
    int pos=0;
    while(pos<20){
    if( )
    sum=sum+arrayList[pos];
     ;
    }
    System.out.println("sum="+sum);
    }
    }
    三、簡單應用題(共24分)
    本題的功能是通過按鈕來選擇窗口顯示的風格。窗口
    中有三個按鈕:“Metal”、“Motif”和“Windows”,單擊任何一
    個按鈕,就能將窗口的風格改變?yōu)榘粹o名稱所對應的風格。
    import java.a(chǎn)wt.*;
    import java.a(chǎn)wt.event.*;
    import javax.swing.*;
    class PlafPanel extends JPanel implements ActionLis-
    tener
    {public ()
    {metaIButton=new JButton("Metal");
    motifButtOn=new J Button("Motif");
    windowsButton=new JButton("Windows");
    add(metalButton);
    add(motifButton);
    add(windowsButton);
    metalButton.a(chǎn)ddActionListener(this);
    motifButton.a(chǎn)ddActionListener(this);
    windowsButton.a(chǎn)ddActionListener(this);
    }
    Dublic void actionPerformed(ActionEvent evt)
    {Object source=evt.getSource();
    String plaf="":
    if(source= =metalButton)
    plaf="javax.swing.plaf.metal.MetalLookAnd-
    Feel";
    else if(source= =motifButton)
    plaf="com.sun.java.swing.plaf.motif.Moti-
    fLookAndFeel";
    else if(source= =windowsButton)
    Dlaf="com.sun.java.swing.plaf.windows.Win-
    dowsLookAndFeel";
    try
    {UIManager.setLookAndFeel( );
    SwingUtilities.updateComponentTreeUI(this);
    }
    catch(Exception e){)
    }
    private JButton metalButton;
    private JButton motifButton;
    private JButton windowsButton;
    }
    class PlafFrame extends JFrame
    {public PlafFrame()
    { setTitle("simple");
    setSize(300,200);
    addWindowListener(new WindowAdapter()
    {public void windowClosing(WindowEvent e)
    {System.exit(O);
    }
    });
    Container contentPane=getContentPane();
    contentPane.a(chǎn)dd(new PlafPanel());
    }
    }
    public class java2
    {public static void main(String[]args)
    f JFrame frame=new PlafFrame();
    frame.show();
    }