「java中级编程题」java常见编程笔试题
今天给各位分享java中级编程题的知识,其中也会对java常见编程笔试题进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
Java的编程题目,在线等,急急急
先做两个比较简单的先用:
import java.util.Arrays;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Function {
/**
* 设计一个方法,完成字符串的解析。方法定义为:public void myParseString(String inputStr);
* 对于给定的字符串,取得字符串内的各个整数(不考虑小数,),然后将取得的数排序,按从小到大依次打印出来。
* @param args
*/
public static void main(String[] args) {
String s = "aa789bB22cc345dd;5.a";
new Function().myParseString(s);
}
public void myParseString(String inputStr){
String mathregix="\\d+";//数字
Vector vector=new Vector();
Pattern fun=Pattern.compile(mathregix);
Matcher match = fun.matcher(inputStr);
while (match.find()) {
vector.add(match.group(0));
}
Object[] obj=vector.toArray();
int[] result=new int[obj.length];
for (int i = 0; i obj.length; i++) {
result[i]=Integer.parseInt((String) obj[i]);
}
Arrays.sort(result);
for (int i = 0; i result.length; i++) {
System.out.println(result[i]);
}
}
}
import java.util.Date;
/**
* 有一个学生类(Student),其有四个私有属性,分别为:
* 学号no,字符串型;
* 姓名name,字符串型;
* 年龄age,整型;
* 生日birthday,日期型;
* 请:
* 1) 按上面描述设计类;
* 2) 定义对每个属性进行取值,赋值的方法;
* 3) 要求学号不能为空,则学号的长度不能少于5位字符,否则抛异常;
* 4) 年龄范围必须在[1,500]之间,否则抛出异常;
*/
public class Student {
private String no;
private String name;
private int age;
private Date birthday;
public int getAge() {
return age;
}
public void setAge(int age) throws Exception {
if(age1||age500)throw new Exception("年龄不合法。");
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) throws Exception {
if(no==null)throw new Exception("学号不能为空!");
if(no.length()5)throw new Exception("学号不能少于五位!");
this.no = no;
}
}
二、三题
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
/**
* 2. 设计一个GUI界面,要求打界面如下:
*点击文件-打开,打开文件选择器,选择打开给定的java.txt文件,将文件内所有a-z,A-Z之间的字符显示在界面的文本区域内。
* 3. 设置一个GUI界面,界面如下:
* 点击文件-保存,打开文件选择窗体,选择一个保存路径,将本界面文本区域内输入的内容进行过滤,将所有非a-z,A-Z之间的字符保存到C盘下的java.txt文件内。
* Java.txt文件内容如下:
* 我们在2009年第2学期学习Java Programming Design,于2009-6-16考试。
*
*
*
*/
public class FileEditer extends javax.swing.JFrame implements ActionListener {
private JMenuBar menubar;
private JMenu file;
private JMenuItem open;
private JTextArea area;
private JMenuItem save;
private JFileChooser jfc;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FileEditer inst = new FileEditer();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public FileEditer() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
area = new JTextArea();
getContentPane().add(area, BorderLayout.CENTER);
area.setText("文本区");
}
{
menubar = new JMenuBar();
setJMenuBar(menubar);
{
file = new JMenu();
menubar.add(file);
file.setText("文件");
file.setPreferredSize(new java.awt.Dimension(66, 21));
{
open = new JMenuItem();
file.add(open);
open.setText("打开");
open.setBorderPainted(false);
open.setActionCommand("open");
open.addActionListener(this);
}
{
save = new JMenuItem();
file.add(save);
save.setActionCommand("save");
save.setText("保存");
save.addActionListener(this);
}
}
}
{
jfc=new JFileChooser();
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if("open".equals(e.getActionCommand())){
int result=jfc.showOpenDialog(this);
if(result==jfc.APPROVE_OPTION){
File file=jfc.getSelectedFile();
try {
byte[] b=new byte[1024];
FileInputStream fis=new FileInputStream(file);
fis.read(b);
fis.close();
String string=new String(b,"GBK");
string=string.replaceAll("[^a-zA-Z]*", "");
area.setText(string.trim());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}
}
}else if("save".equals(e.getActionCommand())){
int result=jfc.showSaveDialog(this);
if(result!=jfc.APPROVE_OPTION)return;
File file=jfc.getSelectedFile();
try {
if(!file.exists()){
file.createNewFile();
}
String string = area.getText();
string=string.replaceAll("[a-zA-Z]*", "");
byte[] b=string.getBytes();
FileOutputStream fis=new FileOutputStream(file);
fis.write(b);
fis.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}
}
}
}
Java编程题
根据题号,代码如下:
1、
public
class
Emp
{
private
String
uName;
private
double
money;
static
final
double
empOp=0.012;//应缴个人所得税
public
String
getuName()
{
return
uName;
}
public
void
setuName(String
uName)
{
this.uName
=
uName;
}
public
double
getMoney()
{
return
money;
}
public
void
setMoney(double
money)
{
this.money
=
money;
}
public
Emp()
{}
public
Emp(String
uName,
double
money)
{
this.uName=uName;
this.money=money;
}
public
String
show()
{
return
"员工姓名:"+getuName()+"\n应缴个人所得税:"+getMoney()*empOp;
}
public
static
void
main(String[]
args)
{
Emp
emp
=
new
Emp("张三",6000);
System.out.println(emp.show());
}
}
2、
public
class
Student
{
private
String
name;
private
int
age;
private
String
degree;
public
String
getName()
{
return
name;
}
public
void
setName(String
name)
{
this.name
=
name;
}
public
int
getAge()
{
return
age;
}
public
void
setAge(int
age)
{
this.age
=
age;
}
public
String
getDegree()
{
return
degree;
}
public
void
setDegree(String
degree)
{
this.degree
=
degree;
}
public
String
show()
{
return
"姓名:"+getName()+"\n年龄:"+getAge()+"\n学位:"+getDegree();
}
public
static
void
main(String[]
args)
{
Graduate
g
=
new
Graduate();
g.setAge(22);
g.setName("张三");
g.setDegree("研究生");
g.setDirection("计算机科学与技术");
System.out.println(g.show());
}
}
class
Undergraduate
extends
Student{
private
String
specialty;
public
String
getSpecialty()
{
return
specialty;
}
public
void
setSpecialty(String
specialty)
{
this.specialty
=
specialty;
}
public
String
show()
{
return
"姓名:"+getName()+"\n年龄:"+getAge()+"\n学位:"+getDegree()+"\n专业:"+getSpecialty();
}
}
class
Graduate
extends
Student{
private
String
direction;
public
String
getDirection()
{
return
direction;
}
public
void
setDirection(String
direction)
{
this.direction
=
direction;
}
public
String
show()
{
return
"姓名:"+getName()+"\n年龄:"+getAge()+"\n学位:"+getDegree()+"\n研究方向:"+getDirection();
}
}
满意请采纳,谢谢!@
JAVA编程题目,共四题,做其中一题就够了,
好了,你测试一下咯!
public interface ShapeArea {//定义ShapeArea接口
public double getArea();//double getArea( ):求一个形状的面积
public double getPerimeter();// Double getPerimeter( ):求一个形状的周长。
}
public class MyTriangle implements ShapeArea {
double x,y,z,s;//x,y,z :double型,表示三角形的三条边
public MyTriangle(double x, double y, double z) {//方法:MyTriangle(double x, double y, double z):构造函数,给三条边和s赋值;
this.x = x;
this.y = y;
this.z = z;
this.s = (x+y+z)/2;
}
@Override
public double getArea() {
return Math.sqrt(this.s*(this.s-this.x)*(this.s-this.y)*(this.s-this.z));
}
@Override
public double getPerimeter() {
return (x+y+z);
}
@Override
public String toString() {
System.out.print("此三角形的面积和周长为:");
return this.getArea()+"、"+this.getPerimeter();
}
}
public class Test {//测试类
public static void main(String[] args) {
MyTriangle myTriangle = new MyTriangle(3, 4, 5);
System.out.println(myTriangle);
}
}
关于java中级编程题和java常见编程笔试题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-21,除非注明,否则均为
原创文章,转载请注明出处。