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;
}
}
下午接著寫