新聞中心
Java實(shí)驗(yàn),代碼怎么寫(xiě)?
Shape.java接口代碼
創(chuàng)新互聯(lián)建站是一家從事企業(yè)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、做網(wǎng)站、行業(yè)門(mén)戶(hù)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)制作的專(zhuān)業(yè)網(wǎng)絡(luò)公司,擁有經(jīng)驗(yàn)豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁(yè)設(shè)計(jì)人員,具備各種規(guī)模與類(lèi)型網(wǎng)站建設(shè)的實(shí)力,在網(wǎng)站建設(shè)領(lǐng)域樹(shù)立了自己獨(dú)特的設(shè)計(jì)風(fēng)格。自公司成立以來(lái)曾獨(dú)立設(shè)計(jì)制作的站點(diǎn)近千家。
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圓類(lèi)代碼
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
? this.radius = radius;
}
@Override
public double area() {
? return PI * this.radius * this.radius;
}
public double perimeter() {
? return 2 * PI * this.radius;
}
}
Cylinder.java圓柱體類(lèi)代碼
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
? super(radius);
? this.height = height;
}
public double area() {
? return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
? return super.area() * this.height;
}
}
X5_3_6.java主類(lèi)代碼
public class X5_3_6 {
public static void main(String[] args) {
? Circle cir1 = new Circle(5);
? System.out.println("圓的面積為:" + cir1.area());
? System.out.println("圓的周長(zhǎng)為:" + cir1.perimeter());
? Cylinder cy1 = new Cylinder(10, 15);
? System.out.println("圓柱體的表面積為:" + cy1.area());
? System.out.println("圓柱體的體積為:" + cy1.volume());
}
}
上面是我寫(xiě)的代碼,下圖是執(zhí)行結(jié)果,麻煩看一下,是否可以。
請(qǐng)高手幫忙解決一個(gè)JAVA實(shí)驗(yàn)程序!!
我把你的第一個(gè)給你貼上來(lái)吧,第二個(gè)應(yīng)該也很簡(jiǎn)單,不過(guò)我懶得麻煩,而且你那個(gè)什么試驗(yàn)五我也不清楚,代碼如下:
interface A{
public int method1(int x);
public int method2(int x,int y);
}
class B implements A{
public int method1(int x) {
// TODO Auto-generated method stub
int n = 1;
for(;x0;x--)
n=n*5;
return n;
}
public int method2(int x, int y) {
// TODO Auto-generated method stub
if(xy)
return y;
else return x;
}
}
public class test{
public static void main(String[] args){
B b=new B();
System.out.println(b.method1(2));
System.out.println(b.method2(2,8));
}
}
我全部都放到一起了,還有如果什么別的你不喜歡的地方,你自己改改,拿分走人。。。。
java實(shí)驗(yàn),一個(gè)時(shí)鐘服務(wù)提供時(shí)間服務(wù),客戶(hù)端發(fā)出請(qǐng)求,服務(wù)器接受請(qǐng)求并向客戶(hù)端發(fā)送當(dāng)前時(shí)間,一小
你好,寫(xiě)一個(gè)client端,代碼如下:
import java.net.*;
import java.io.*;
public class DateClient {
static final int LISTENING_PORT = 32007;
public static void main(String[] args) {
String computer; // Name of the computer to connect to.
Socket connection; // A socket for communicating with
// that computer.
Reader incoming; // Stream for reading data from
// the connection.
/* Get computer name from command line. */
if (args.length 0)
computer = args[0];
else {
// No computer name was given. Print a message and exit.
System.out.println("Usage: java DateClient server");
return;
}
/* Make the connection, then read and display a line of text. */
try {
connection = new Socket( computer, LISTENING_PORT );
incoming = new InputStreamReader( connection.getInputStream() );
while (true) {
int ch = incoming.read();
if (ch == -1 || ch == '\n' || ch == '\r')
break;
System.out.print( (char)ch );
}
System.out.println();
incoming.close();
}
catch (IOException e) {
TextIO.putln("Error: " + e);
}
// end main()
} // end class DateClient
再寫(xiě)一個(gè)服務(wù)端,代碼如下:
import java.net.*;
import java.io.*;
import java.util.Date;
public class DateServe {
static final int LISTENING_PORT = 32007;
public static void main(String[] args) {
ServerSocket listener; // Listens for incoming connections.
Socket connection; // For communication with the
// connecting program.
/* Accept and process connections forever, or until
some error occurs. (Note that errors that occur
while communicating with a connected program are
caught and handled in the sendDate() routine, so
they will not crash the server.)
*/
try {
listener = new ServerSocket(LISTENING_PORT);
TextIO.putln("Listening on port " + LISTENING_PORT);
while (true) {
connection = listener.accept();
sendDate(connection);
}
}
catch (Exception e) {
TextIO.putln("Sorry, the server has shut down.");
TextIO.putln("Error: " + e);
return;
}
} // end main()
static void sendDate(Socket client) {
// The parameter, client, is a socket that is
// already connected to another program. Get
// an output stream for the connection, send the
// current date, and close the connection.
try {
System.out.println("Connection from " +
client.getInetAddress().toString() );
Date now = new Date(); // The current date and time.
PrintWriter outgoing; // Stream for sending data.
outgoing = new PrintWriter( client.getOutputStream() );
outgoing.println( now.toString() );
outgoing.flush(); // Make sure the data is actually sent!
client.close();
}
catch (Exception e){
System.out.println("Error: " + e);
}
} // end sendDate()
} //end class DateServe
發(fā)起客戶(hù)端請(qǐng)求就可以得到服務(wù)器時(shí)間了!
--溫馨提示--
真心希望你能采納我的回答,如有不明白,可以繼續(xù)追問(wèn),若滿(mǎn)意,記得及時(shí)采納。
Java計(jì)算器實(shí)驗(yàn)報(bào)告(含代碼),急?。。?!
給你一個(gè)吧。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 一個(gè)計(jì)算器,與Windows附件自帶計(jì)算器的標(biāo)準(zhǔn)版功能、界面相仿。
* 但還不支持鍵盤(pán)操作。
*/
public class Calculator extends JFrame implements ActionListener {
/** 計(jì)算器上的鍵的顯示名字 */
private final String[] KEYS = { "7", "8", "9", "/", "sqrt", "4", "5", "6",
"*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
/** 計(jì)算器上的功能鍵的顯示名字 */
private final String[] COMMAND = { "Backspace", "CE", "C" };
/** 計(jì)算器左邊的M的顯示名字 */
private final String[] M = { " ", "MC", "MR", "MS", "M+" };
/** 計(jì)算器上鍵的按鈕 */
private JButton keys[] = new JButton[KEYS.length];
/** 計(jì)算器上的功能鍵的按鈕 */
private JButton commands[] = new JButton[COMMAND.length];
/** 計(jì)算器左邊的M的按鈕 */
private JButton m[] = new JButton[M.length];
/** 計(jì)算結(jié)果文本框 */
private JTextField resultText = new JTextField("0");
// 標(biāo)志用戶(hù)按的是否是整個(gè)表達(dá)式的第一個(gè)數(shù)字,或者是運(yùn)算符后的第一個(gè)數(shù)字
private boolean firstDigit = true;
// 計(jì)算的中間結(jié)果。
private double resultNum = 0.0;
// 當(dāng)前運(yùn)算的運(yùn)算符
private String operator = "=";
// 操作是否合法
private boolean operateValidFlag = true;
/**
* 構(gòu)造函數(shù)
*/
public Calculator(){
super();
//初始化計(jì)算器
init();
//設(shè)置計(jì)算器的背景顏色
this.setBackground(Color.LIGHT_GRAY);
this.setTitle("計(jì)算器");
//在屏幕(500, 300)坐標(biāo)處顯示計(jì)算器
this.setLocation(500, 300);
//不許修改計(jì)算器的大小
this.setResizable(false);
//使計(jì)算器中各組件大小合適
this.pack();
}
/**
* 初始化計(jì)算器
*/
private void init() {
// 文本框中的內(nèi)容采用右對(duì)齊方式
resultText.setHorizontalAlignment(JTextField.RIGHT);
// 不允許修改結(jié)果文本框
resultText.setEditable(false);
// 設(shè)置文本框背景顏色為白色
resultText.setBackground(Color.white);
//初始化計(jì)算器上鍵的按鈕,將鍵放在一個(gè)畫(huà)板內(nèi)
JPanel calckeysPanel = new JPanel();
//用網(wǎng)格布局器,4行,5列的網(wǎng)格,網(wǎng)格之間的水平方向間隔為3個(gè)象素,垂直方向間隔為3個(gè)象素
calckeysPanel.setLayout(new GridLayout(4, 5, 3, 3));
for (int i = 0; i KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
calckeysPanel.add(keys[i]);
keys[i].setForeground(Color.blue);
}
//運(yùn)算符鍵用紅色標(biāo)示,其他鍵用藍(lán)色表示
keys[3].setForeground(Color.red);
keys[8].setForeground(Color.red);
keys[13].setForeground(Color.red);
keys[18].setForeground(Color.red);
keys[19].setForeground(Color.red);
//初始化功能鍵,都用紅色標(biāo)示。將功能鍵放在一個(gè)畫(huà)板內(nèi)
JPanel commandsPanel = new JPanel();
//用網(wǎng)格布局器,1行,3列的網(wǎng)格,網(wǎng)格之間的水平方向間隔為3個(gè)象素,垂直方向間隔為3個(gè)象素
commandsPanel.setLayout(new GridLayout(1, 3, 3, 3));
for (int i = 0; i COMMAND.length; i++) {
commands[i] = new JButton(COMMAND[i]);
commandsPanel.add(commands[i]);
commands[i].setForeground(Color.red);
}
//初始化M鍵,用紅色標(biāo)示,將M鍵放在一個(gè)畫(huà)板內(nèi)
JPanel calmsPanel = new JPanel();
//用網(wǎng)格布局管理器,5行,1列的網(wǎng)格,網(wǎng)格之間的水平方向間隔為3個(gè)象素,垂直方向間隔為3個(gè)象素
calmsPanel.setLayout(new GridLayout(5, 1, 3, 3));
for (int i = 0; i M.length; i++) {
m[i] = new JButton(M[i]);
calmsPanel.add(m[i]);
m[i].setForeground(Color.red);
}
//下面進(jìn)行計(jì)算器的整體布局,將calckeys和command畫(huà)板放在計(jì)算器的中部,
//將文本框放在北部,將calms畫(huà)板放在計(jì)算器的西部。
//新建一個(gè)大的畫(huà)板,將上面建立的command和calckeys畫(huà)板放在該畫(huà)板內(nèi)
JPanel panel1 = new JPanel();
//畫(huà)板采用邊界布局管理器,畫(huà)板里組件之間的水平和垂直方向上間隔都為3象素
panel1.setLayout(new BorderLayout(3, 3));
panel1.add("North", commandsPanel);
panel1.add("West", calckeysPanel);
//建立一個(gè)畫(huà)板放文本框
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
top.add("Center", resultText);
//整體布局
getContentPane().setLayout(new BorderLayout(3, 5));
getContentPane().add("North", top);
getContentPane().add("Center", panel1);
getContentPane().add("West", calmsPanel);
//為各按鈕添加事件偵聽(tīng)器
//都使用同一個(gè)事件偵聽(tīng)器,即本對(duì)象。本類(lèi)的聲明中有implements ActionListener
for (int i = 0; i KEYS.length; i++) {
keys[i].addActionListener(this);
}
for (int i = 0; i COMMAND.length; i++) {
commands[i].addActionListener(this);
}
for (int i = 0; i M.length; i++) {
m[i].addActionListener(this);
}
}
/**
* 處理事件
*/
public void actionPerformed(ActionEvent e) {
//獲取事件源的標(biāo)簽
String label = e.getActionCommand();
if (label.equals(COMMAND[0])){
//用戶(hù)按了"Backspace"鍵
handleBackspace();
} else if (label.equals(COMMAND[1])) {
//用戶(hù)按了"CE"鍵
resultText.setText("0");
} else if (label.equals(COMMAND[2])){
//用戶(hù)按了"C"鍵
handleC();
} else if ("0123456789.".indexOf(label) = 0) {
//用戶(hù)按了數(shù)字鍵或者小數(shù)點(diǎn)鍵
handleNumber(label);
// handlezero(zero);
} else {
//用戶(hù)按了運(yùn)算符鍵
handleOperator(label);
}
}
/**
* 處理Backspace鍵被按下的事件
*/
private void handleBackspace() {
String text = resultText.getText();
int i = text.length();
if (i 0) {
//退格,將文本最后一個(gè)字符去掉
text = text.substring(0, i - 1);
if (text.length() == 0) {
//如果文本沒(méi)有了內(nèi)容,則初始化計(jì)算器的各種值
resultText.setText("0");
firstDigit = true;
operator = "=";
} else {
//顯示新的文本
resultText.setText(text);
}
}
}
/**
* 處理數(shù)字鍵被按下的事件
* @param key
*/
private void handleNumber(String key) {
if (firstDigit) {
//輸入的第一個(gè)數(shù)字
resultText.setText(key);
} else if ((key.equals(".")) (resultText.getText().indexOf(".") 0)){
//輸入的是小數(shù)點(diǎn),并且之前沒(méi)有小數(shù)點(diǎn),則將小數(shù)點(diǎn)附在結(jié)果文本框的后面
resultText.setText(resultText.getText() + ".");
} else if (!key.equals(".")) {
//如果輸入的不是小數(shù)點(diǎn),則將數(shù)字附在結(jié)果文本框的后面
resultText.setText(resultText.getText() + key);
}
//以后輸入的肯定不是第一個(gè)數(shù)字了
firstDigit = false;
}
/**
* 處理C鍵被按下的事件
*/
private void handleC() {
//初始化計(jì)算器的各種值
resultText.setText("0");
firstDigit = true;
operator = "=";
}
/**
* 處理運(yùn)算符鍵被按下的事件
* @param key
*/
private void handleOperator(String key) {
if (operator.equals("/")) {
//除法運(yùn)算
//如果當(dāng)前結(jié)果文本框中的值等于0
if (getNumberFromText() == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("除數(shù)不能為零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("1/x")) {
//倒數(shù)運(yùn)算
if (resultNum == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("零沒(méi)有倒數(shù)");
} else {
resultNum = 1 / resultNum;
}
} else if (operator.equals("+")){
//加法運(yùn)算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
//減法運(yùn)算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
//乘法運(yùn)算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
//平方根運(yùn)算
resultNum = Math.sqrt(resultNum);
} else if (operator.equals("%")){
//百分號(hào)運(yùn)算,除以100
resultNum = resultNum / 100;
} else if (operator.equals("+/-")){
//正數(shù)負(fù)數(shù)運(yùn)算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
//賦值運(yùn)算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
//雙精度浮點(diǎn)數(shù)的運(yùn)算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(resultNum));
}
}
//運(yùn)算符等于用戶(hù)按的按鈕
operator = key;
firstDigit = true;
operateValidFlag = true;
}
/**
* 從結(jié)果文本框中獲取數(shù)字
* @return
*/
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e){
}
return result;
}
public static void main(String args[]) {
Calculator calculator1 = new Calculator();
calculator1.setVisible(true);
calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
求JAVA實(shí)驗(yàn)代碼
public interface Student {
// 該方法用于表示不同階段的學(xué)生在學(xué)習(xí)數(shù)學(xué)課程時(shí)的不同內(nèi)容
public abstract void studyMath();
// 該方法用于表示不同階段的學(xué)生的英語(yǔ)水平
public abstract void studyEnglish();
}
public class PrimarySchoolStudent implements Student {
@Override
public void studyMath() {
System.out.println("小學(xué)生在學(xué)習(xí)數(shù)學(xué)課程時(shí),主要學(xué)習(xí)加減法,數(shù)學(xué)表達(dá)式等基礎(chǔ)知識(shí)。");
}
@Override
public void studyEnglish() {
System.out.println("小學(xué)生在學(xué)習(xí)英語(yǔ)時(shí),主要學(xué)習(xí)詞匯,基本句型,基本語(yǔ)法等基礎(chǔ)知識(shí)。");
}
}
public class MiddleSchoolStudent implements Student {
@Override
public void studyMath() {
System.out.println("中學(xué)生在學(xué)習(xí)數(shù)學(xué)課程時(shí),主要學(xué)習(xí)初等函數(shù),代數(shù)方程等基礎(chǔ)知識(shí)。");
}
@Override
public void studyEnglish() {
System.out.println("中學(xué)生在學(xué)習(xí)英語(yǔ)時(shí),主要學(xué)習(xí)閱讀理解,聽(tīng)力理解,口語(yǔ)交流等能力。");
}
}
public class CollegeStudent implements Student {
@Override
public void studyMath() {
System.out.println("大學(xué)生在學(xué)習(xí)數(shù)學(xué)課程時(shí),主要學(xué)習(xí)高等數(shù)學(xué),概率論,數(shù)值計(jì)算等專(zhuān)業(yè)知識(shí)。");
}
@Override
public void studyEnglish() {
System.out.println("大學(xué)生在學(xué)習(xí)英語(yǔ)時(shí),主要學(xué)習(xí)專(zhuān)業(yè)英語(yǔ),商務(wù)英語(yǔ),英文寫(xiě)作等能力。");
}
}
public class Main {
public static void main(String[] args) {
求解一道Java實(shí)驗(yàn)題,給出一段代碼,要求把該代碼補(bǔ)充完整使其可以運(yùn)行,具體要求如下
package xinguan;
abstract class Operation{ //抽象類(lèi)
public static double numberA= 0;
public static double numberB = 0;
abstract double getResult(); //抽象方法
}
class OperationADD extends Operation{
@Override
double getResult() {
return numberA+numberB;
}
}
class OperationSUB extends Operation{
@Override
double getResult() {
return numberA-numberB;
}
}
class OperationMUL extends Operation{
@Override
double getResult() {
return numberA*numberB;
}
}
class OperationDIV extends Operation{
@Override
double getResult() {
return numberA/numberB;
}
}
class OperationFactory{
public static Operation createOperate(char operate){
Operation oper = null;
switch (operate){
case'+':
oper= new OperationADD();
break;
case'-':
oper= new OperationSUB();
break;
case'*':
oper= new OperationMUL();
break;
case'/':
oper= new OperationDIV();
break;
}
return oper;
}
}
public class CalculateDemo {
/**
* @param args
*/
public static void main(String[] args) {
Operation operADD = OperationFactory.createOperate('+');
Operation operSUB = OperationFactory.createOperate('-');
Operation operMUL = OperationFactory.createOperate('*');
Operation operDIV = OperationFactory.createOperate('/');
operADD.numberA = 15.0;
operADD.numberB = 3;
System.out.println(operADD.getResult());
System.out.println(operSUB.getResult());
System.out.println(operMUL.getResult());
System.out.println(operDIV.getResult());
}
}
因?yàn)槌橄箢?lèi)是靜態(tài)方法 所以 給operADD 那個(gè)對(duì)象賦值一次就能獲得所有結(jié)果。要是去掉static 那么就需要每個(gè)對(duì)象 賦值?,F(xiàn)在基本滿(mǎn)足你的要求了。
新聞名稱(chēng):蘇州大學(xué)java實(shí)驗(yàn)代碼 蘇州大學(xué) 代碼
文章URL:http://ef60e0e.cn/article/hjoioj.html