class Circle {
/**
* 作者:lzxianren
* 目的:求圆面积的类,里面有一个私有变量的半径,一个求面积的方法,一个有参构造方法;
*/
private double radius;
public double getSquare(){
return Math.PI*radius*radius;
}
public Circle(double radius){
this.radius=radius;
}
}
class Cylinder {
private double radius;
private double high;
public Cylinder(double radius,double high){
this.high=high;
this.radius=radius;
}
public double getSquare(){
return (Math.PI*radius*radius*2+Math.PI*radius*2*high);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Circle c1=new Circle(1.1);
Cylinder cy1=new Cylinder(1.1,2.2);
double c1Square=c1.getSquare();
double cy1Square=cy1.getSquare();
double sum=c1Square+cy1Square;
System.out.println("半径为1.1的圆的面积为"+c1Square+" 半径为1.1高为2.2的圆柱的表面积为"+cy1Square+" 面积之和为"+sum);
}
}
㈡ java编程题 本人新手,求详解。
先看下最终的结果吧,是不是你想要的?
其中,Student是父类,PostGraate是子类,继承自父类Student,Main是主类,用于创建对象以及把这些对象的功能调用起来。
---------------------------Student代码如下:------------------------------
/**
* 学生类
* @author 逍遥
*
*/
public class Student {
//学号
private int sId;
//姓名
private String sName;
//数学成绩
private double mathScore;
//计算机成绩
private double computerScore;
/**
* 获取学号
* @return
*/
public int getsId() {
return sId;
}
/**
* 设置学号
* @param sId
*/
public void setsId(int sId) {
this.sId = sId;
}
/**
* 获取姓名
* @return
*/
public String getsName() {
return sName;
}
/**
* 设置姓名
* @param sName
*/
public void setsName(String sName) {
this.sName = sName;
}
/**
* 获取数学成绩
* @return
*/
public double getMathScore() {
return mathScore;
}
/**
* 设置数学成绩
* @param mathScore
*/
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
/**
* 获取计算机成绩
* @return
*/
public double getComputerScore() {
return computerScore;
}
/**
* 设置计算机成绩
* @param computerScore
*/
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
}
/**
* 输出成员变量(4个成员变量)的信息。
*/
public void print(){
System.out.println("学号:"+sId);
System.out.println("姓名:"+sName);
System.out.println("计算机成绩:"+mathScore);
System.out.println("数学成绩:"+computerScore);
}
}
---------------------------Student代码结束------------------------------
---------------------------PostGraate代码如下:------------------------------
/**
* 研究生类
* @author 逍遥
*
*/
public class PostGraate extends Student{
//导师姓名
private String tName;
//研究方向
private String ResearchDirection;
/**
* 获取导师姓名
* @return
*/
public String gettName() {
return tName;
}
/**
* 设置导师姓名
* @param tName
*/
public void settName(String tName) {
this.tName = tName;
}
/**
* 获取研究方向
* @return
*/
public String getResearchDirection() {
return ResearchDirection;
}
/**
* 设置研究方向
* @param researchDirection
*/
public void setResearchDirection(String researchDirection) {
ResearchDirection = researchDirection;
}
/**
* 研究生类重写父类的void print()方法,功能是输出成员变量(6个成员变量)的信息
*/
@Override
public void print() {
// TODO Auto-generated method stub
super.print();
System.out.println("导师姓名:"+tName);
System.out.println("研究方向:"+ResearchDirection);
}
}
---------------------------PostGraate代码结束------------------------------
---------------------------Main代码如下:------------------------------
import java.util.Scanner;
/**
* 主类
* @author 逍遥
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用于获取从键盘上输入的信息
Scanner input=new Scanner(System.in);
//创建一个Student类的对象
Student student=new Student();
//从键盘上输入其属性信息
System.out.print("请输入学生的学号:");
student.setsId(input.nextInt());
System.out.print("请输入学生的姓名:");
student.setsName(input.next());
System.out.print("请输入学生的数学成绩:");
student.setMathScore(input.nextDouble());
System.out.print("请输入学生的计算机成绩:");
student.setComputerScore(input.nextDouble());
//并且通过其print方法输出这些信息;
student.print();
//创建一个PostGraate类的对象
PostGraate postGraate=new PostGraate();
//从键盘上输入其属性信息
System.out.print("请输入研究生的学号:");
postGraate.setsId(input.nextInt());
System.out.print("请输入研究生的姓名:");
postGraate.setsName(input.next());
System.out.print("请输入研究生的数学成绩:");
postGraate.setMathScore(input.nextDouble());
System.out.print("请输入研究生的计算机成绩:");
postGraate.setComputerScore(input.nextDouble());
System.out.print("请输入研究生的导师姓名:");
postGraate.settName(input.next());
System.out.print("请输入研究生的研究方向:");
postGraate.setResearchDirection(input.next());
//并且通过其print方法输出这些信息。
postGraate.print();
}
}
---------------------------Main代码结束------------------------------
=================知识点的简单总结=================
本题考察的知识点是面向对象的三大特性之一:继承。
Student为父类,包含了学号、姓名、数学成绩和计算机成绩4个属性,以及一个print()方法。
PostGraate 继承父类的时候,继承了父类中的所有方法,因为方法我都是用的public,而属性继承不了,因为我在父类中用了封装,所有属性都用private修饰了,想访问属性的话,必须通过get、set方法,这里,我重写了父类中的print方法,通过super.print();调用了父类中的print()方法。
最后就是Main类,提供了main方法作为入口函数,用于按要求声明这些对象以及去调用对象中的方法。
㈢ java编程题目
这不都说的很清楚了么。。。。。。。。
自己写吧,也没啥难度。
是完全不知道这个题目再说什么么?
packagespring5.source;
importjava.awt.Button;
importjava.awt.FlowLayout;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjavax.swing.JFrame;
publicclassDextendsJFrame{
publicstaticvoidmain(String[]args){
Dd=newD();
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.setSize(500,500);
d.setLayout(newFlowLayout());
TextFieldt1=newTextField();
TextFieldt2=newTextField();
Buttonb=newButton("OK");
b.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Stringv1=t1.getText();
try{
intn=Integer.parseInt(v1);
Doubled=Math.pow(n,2);
t2.setText(String.valueOf(d.intValue()));
}catch(Exceptione2){
e2.printStackTrace();
}
}
});
t1.addKeyListener(newKeyListener(){
@Override
publicvoidkeyTyped(KeyEvente){
}
@Override
publicvoidkeyReleased(KeyEvente){
}
@Override
publicvoidkeyPressed(KeyEvente){
if(e.getKeyChar()==KeyEvent.VK_ENTER){
Stringv1=t1.getText();
try{
intn=Integer.parseInt(v1);
Doubled=Math.pow(n,2);
t2.setText(String.valueOf(d.intValue()));
}catch(Exceptione2){
e2.printStackTrace();
}
}
}
});
// KeyListenerkey_Listener=;
d.add(t1);
d.add(t2);
d.add(b);
d.setVisible(true);
}
}
少了一个d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);关闭窗口的
㈣ java上机试题
(1) package Fruits;//声明包语句
(2) import java.util.*;
proceDate = (3) new Date();//
(4) this.weight = weight;//为数据成员weight赋初值
public (5)abstract String getInfo();//定义抽象方法
class Apple (6) extends Fruit (7)implements Edible
(8)static int count = 0;//count负责记录Fruit对象的数量
(9)super(weight);//调用父类的构造函数
(10)public String getInfo() //重写抽象类中的抽象方法
㈤ 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(age<1||age<500)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
importjava.util.Scanner;
publicclasstest1{
privatestaticScannerinput=newScanner(System.in);
publicstaticvoidmain(String[]args){
longnum;
do{
System.out.print(" Inputyournum:");
num=input.nextLong();
}while(num<10000||num>99999);
num=num/100;
num*=100;
System.out.println(" Thisisnum:"+num);
}
}
2.
importjava.util.Scanner;
publicclasstest2{
privatestaticScannerinput=newScanner(System.in);
publicstaticvoidmain(String[]args){
longnum;
do{
System.out.print(" Inputyournum:");
num=input.nextLong();
}while(num<0||num>1000);
intsum=0;
while(num>0){
sum+=num%10;
num/=10;
}
System.out.println(" Thisissum:"+sum);
}
}
㈦ java编程题(写出代码)
代码如下:
classBox{
privateintlength;
privateintwidth;
privateintheight;
publicvoidsetBox(intl,intw,inth){
this.length=l;
this.width=w;
this.height=h;
}
publicintvolume(){
returnlength*width*height;
}
}
publicclassApp{
publicstaticvoidmain(String[]argv){
Boxb=newBox();
b.setBox(3,4,5);
System.out.println("体积:"+b.volume());
}
}
㈧ JAVA编程题求全部代码
classHW1{
publicstaticvoidmain(String[]args){
double[]test=newdouble[]{5.2,1.0,6.7,3.4,100.5,55.5};
BoundryValuesboundryValues=getBoundryValues(test);
System.out.println("MinValue="+boundryValues.getMin());
System.out.println("MaxValue="+boundryValues.getMax());
System.out.println("AveValue="+boundryValues.getAve());
}
{
privatedoublemax;
privatedoublemin;
privatedoubleave;
publicBoundryValues(){}
publicBoundryValues(doublemax,doublemin,doubleave){
this.max=max;
this.min=min;
this.ave=ave;
}
publicvoidsetMax(doublemax){
this.max=max;
}
publicdoublegetMax(){
returnmax;
}
publicvoidsetMin(doublemin){
this.min=min;
}
publicdoublegetMin(){
returnmin;
}
publicvoidsetAve(doubleave){
this.ave=ave;
}
publicdoublegetAve(){
returnave;
}
}
(double[]doubles){
BoundryValuesboundryValues=newBoundryValues();
double[]results=sort(doubles);
doubletotal=0.0;
for(inti=0;i<results.length;i++){
total+=results[i];
}
boundryValues.setMin(results[0]);
boundryValues.setMax(results[results.length-1]);
boundryValues.setAve(total/results.length);
returnboundryValues;
}
publicstaticdouble[]sort(double[]doubles){
for(inti=0;i<doubles.length;i++){
for(intj=0;j<doubles.length-i-1;j++){
if(doubles[j]>doubles[j+1]){
doubletemp=doubles[j];
doubles[j]=doubles[j+1];
doubles[j+1]=temp;
}
}
}
returndoubles;
}
}
importjava.util.*;
classHW2{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
doublea,b,c;
System.out.println("Entera,b,c:");
a=scanner.nextDouble();
b=scanner.nextDouble();
c=scanner.nextDouble();
Set<Double>sets=calculate(a,b,c);
for(Doubled:sets){
System.out.println("Valuesare:"+d+" ");
}
}
publicstaticSet<Double>calculate(doublea,doubleb,doublec){
Set<Double>sets=newHashSet<Double>();
if(Math.pow(b,2.0)-4*a*c<0){
System.err.println("Novalue");
}else{
sets.add((-b+Math.sqrt(Math.pow(b,2.0)-4*a*c))/2.0*a);
sets.add((-b-Math.sqrt(Math.pow(b,2.0)-4*a*c))/2.0*a);
}
returnsets;
}
}
下午接着写