java前三次作業總結(java第三章總結)

本文目錄一覽:

高級語言程序設計(基於JAVA)階段性作業(三)高分求助!!

1.

interface A

{void print();}

2.

重載就是一個類中有多個同名但有不同形參和方法體的方法

3.

某個非抽象類的父類是抽象類,則這個子類必須重載父類的所有抽象方法

4.

重複代碼

5.

構造函數與類同名

6.

可以讓方法返回數值

7.

方法沒有返回值

8.

可以沒有

9.

可能在方法內的局部可見

java作業

這個要求很明顯了,使用class關鍵字創建類,同時使用集合類ArrayList來創建對象,並添加元素,然後輸出,輸出可以直接輸出集合對象,可以使用forEach循環輸出,可以使用迭代器Iterator來輸出等,只要注意集合是沒有元素類型,所有輸出的都是Object類型的數據,下面的例子僅作為參考:

import java.util.*;

//創建一個圖書Book類

class Book

{

String s;

public Book(String s){

this.s = s;

}

//重寫toString方法,使其輸出指定格式的數據

public String toString(){

return “Book[s : ” + s + “]”;

}

}

//創建一個學生Student類

class Student

{

String s;

public Student(String s){

this.s = s;

}

//重寫toString方法,使其輸出指定格式的數據

public String toString(){

return “Student[s:” + s + “]”;

}

}

//測試類

public class  Example1

{

public static void main(String[] args) 

{

//創建兩個圖書對象

Book b1 = new Book(“圖書類第一個對象”);

Book b2 = new Book(“圖書類第二個對象”);

//創建兩個學生類對象

Student s1 = new Student(“學生類第一個對象”);

Student s2 = new Student(“學生類第二個對象”);

//創建一個ArrayList對象

ArrayList a = new ArrayList();

//調用ArrayList類的add()方法,將上面創建的對象添加進a對象中

a.add(b1);

a.add(b2);

a.add(s1);

a.add(s2);

//通過使用forEach方法輸出a對象中的元素,因為ArrayListy是集合類

//集合類中的元素是沒有類型,輸出的全部都是Object類型

for(Object o : a){

System.out.println(o);

}

//也可以直接輸出ArrayList對象中的元素

System.out.println(a);

}

}

java小作業

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Label;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.TextEvent;

import java.awt.event.TextListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class Exer07_4 {

public static void main(String arg[]) {

WindowText4 win = new WindowText4(“計算的窗口”);

}

}

class WindowText4 extends Frame implements TextListener {

TextField text1, text2;

Label label1;

WindowText4(String s) {

super(s);

setLayout(new FlowLayout());

addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

label1 = new Label(“輸入完數字後請按 enter! “, Label.CENTER);

label1.setBackground(Color.cyan);

text1 = new TextField(10);

text2 = new TextField(20);

text1.addTextListener(this);

add(label1);

add(text1);

add(text2);

setBounds(150, 150, 250, 200);

this.setVisible(true);

validate();

}

public void textValueChanged(TextEvent e) {

label1.setText(“輸入完數字後請按enter!”);

if (e.getSource() == text1) {

String str = text1.getText();

Integer sum = 0;

Integer num = 0;

float avg = 0;

for (int i = 1; i = str.length(); i++) {

try {

num = Integer.parseInt(str.substring(i – 1, i));

sum += num;

avg = (float) sum / str.length();

text2.setText(“和:” + sum + ” 平均值:” + avg);

} catch (Exception ex) {

label1.setText(“輸入錯誤,重新輸入後按enter!”);

}

//text1.setText(“”);

}

}

}

}

JAVA作業

只有代碼沒有文檔。是用棧的知識實現的,沒學過的話就沒辦法了。

————————————–

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

class Stack_Float

{

float nums[];

int top;

Stack_Float()

{

nums = new float[50];

top = -1;

}

boolean IsEmpty()

{

if (top == -1)

return true;

else

return false;

}

float Pop_Stack()

{

if (top == -1)

{

return 0;

}

top–;

return nums[top + 1];

}

float GetTop()

{

return nums[top];

}

void Push_Stack(float num)

{

if (top == 49)

return;

top++;

nums[top] = num;

}

}

class Stack_Char

{

char str[];

int top;

Stack_Char()

{

str = new char[50];

top = -1;

}

boolean CanPush(char c)

{

int temp = top;

if (c == ‘(‘)

{

while (temp != -1)

{

if (str[temp] == ‘(‘)

{

return false;

}

temp–;

}

}

temp = top;

if (c == ‘[‘)

{

while (temp != -1)

{

if (str[temp] == ‘[‘ || str[temp] == ‘(‘)

{

return false;

}

temp–;

}

}

if (c == ‘{‘)

{

while (temp != -1)

{

if (str[temp] == ‘{‘ || str[temp] == ‘[‘ || str[temp] == ‘(‘)

{

return false;

}

temp–;

}

}

return true;

}

boolean IsEmpty()

{

if (top == -1)

return true;

else

return false;

}

void Push_Stack(char ch)

{

if (top == 49)

return;

top++;

str[top] = ch;

}

char Pop_Stack()

{

if (top == -1)

return ‘\0’;

top–;

return str[top + 1];

}

char GetTop()

{

if (top == -1)

{

System.out.print(“error”);

System.exit(0);

}

return str[top];

}

}

public class jisuanqi extends javax.swing.JFrame implements ActionListener

{

JTextField text = new JTextField();

JTextField text1 = new JTextField();

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

JButton jButton3 = new JButton();

JButton jButton4 = new JButton();

JButton jButton5 = new JButton();

JButton jButton6 = new JButton();

JButton jButton7 = new JButton();

JButton jButton8 = new JButton();

JButton jButton9 = new JButton();

JButton jButton10 = new JButton();

JButton jButton11 = new JButton();

JButton jButton12 = new JButton();

JButton jButton13 = new JButton();

JButton jButton14 = new JButton();

JButton jButton15 = new JButton();

JButton jButton16 = new JButton();

JButton jButton17 = new JButton();

JButton jButton18 = new JButton();

JButton jButton19 = new JButton();

JButton jButton20 = new JButton();

JButton jButton21 = new JButton();

JButton jButton22 = new JButton();

String show = “”;

public jisuanqi()

{

initComponents();

}

char[] TranSmit(char str[])

{

char houzhui[] = new char[50]; // 存放後綴表達式的字元串

int i = 0, j = 0;

char c = str[i];

Stack_Char s = new Stack_Char(); // 存放運算符的棧

while (c != ‘=’) // 對算術表達式掃描未結束時

{

if (c = ‘0’ c = ‘9’)

{

while (c = ‘0’ c = ‘9’)// 數字直接入棧

{

houzhui[j] = c;

j++;

i++;

c = str[i];

}

houzhui[j] = ‘#’;// 用#隔開數字

j++;

}

switch (c) // 掃描到運算符時

{

case ‘+’:

case ‘-‘:

case ‘*’:

case ‘/’:

case ‘(‘:

case ‘[‘:

case ‘{‘:

if (s.IsEmpty() == true) // 棧空,直接入棧

{

s.Push_Stack(c);

i++;

c = str[i];

break;

}

if (ComPare(s.GetTop(), c) == -1) {

s.Push_Stack(c); // 入棧

i++;

c = str[i];

break;

}

if (ComPare(s.GetTop(), c) == 1) {

houzhui[j] = s.Pop_Stack();// 出棧元素存入後綴表達式

j++;

break;

}

case ‘)’: // 掃描到 )

while (s.GetTop() != ‘(‘) // 未掃描到 ( 時,出棧

{

houzhui[j] = s.Pop_Stack();

j++;

}

s.Pop_Stack(); // ‘(‘ 出棧

i++;

c = str[i];

break;

case ‘]’: // 掃描到 ]

while (s.GetTop() != ‘[‘) // 未掃描到 [ 時,出棧

{

houzhui[j] = s.Pop_Stack();

j++;

}

s.Pop_Stack(); // ‘[‘ 出棧

i++;

c = str[i];

break;

case ‘}’: // 掃描到 }

while (s.GetTop() != ‘{‘) // 未掃描到 { 時,出棧

{

houzhui[j] = s.Pop_Stack();

j++;

}

s.Pop_Stack(); // ‘{‘ 出棧

i++;

c = str[i];

break;

}

}

while (s.IsEmpty() != true)// 把剩餘的運算符直接出棧

{

houzhui[j] = s.Pop_Stack();

j++;

}

houzhui[j] = ‘=’;// 後綴表達式後面加 =

j++;

houzhui[j] = ‘\0’;

j++;

return houzhui;

}

float Count(char str[])

{

Stack_Float s = new Stack_Float();// 定義存放數字的棧

char c = str[0];

int i = 0;

float result = 0, temp, left, right;

while (c != ‘=’) // 未掃描到 = 時

{

if (c = ‘0’ c = ‘9’)// 掃描到數字

{

temp = 0;

while (c != ‘#’)// 未讀到分隔符時

{

temp = temp * 10 + c – ‘0’;

i++;

c = str[i];

}

s.Push_Stack(temp);// 進棧

}

switch (c)// 掃描到運算符時

{

case ‘+’:

{

result = s.Pop_Stack() + s.Pop_Stack();// 2個數字出棧相加

s.Push_Stack(result);// 最後得數進棧

break;

}

case ‘-‘:

{

right = s.Pop_Stack();// 右操作數出棧

left = s.Pop_Stack();// 左操作數出棧

result = left – right;

s.Push_Stack(result);

break;

}

case ‘*’:

{

result = s.Pop_Stack() * s.Pop_Stack();// 2個數字出棧相乘

s.Push_Stack(result);

break;

}

case ‘/’:

{

right = s.Pop_Stack();// 右操作數出棧

left = s.Pop_Stack();// 左操作數出棧

result = left / right;

s.Push_Stack(result);

break;

}

}

i++;

c = str[i];

}

return result;

}

int ComPare(char a, char b) // 判斷運算符的優先順序函數

{

int s[][] = {// 棧頂元素高於算術表達式中的元素時, 返回 1,否則返回 -1

{ 1, 1, -1, -1, -1, 1, -1, 1, -1, 1 },

{ 1, 1, -1, -1, -1, 1, -1, 1, -1, 1 },

{ 1, 1, 1, 1, -1, 1, -1, 1, -1, 1 },

{ 1, 1, 1, 1, -1, 1, -1, 1, -1, 1 },

{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },

{ 1, 1, 1, 1, -1, 1, -1, -1, -1, -1 },

{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },

{ 1, 1, 1, 1, -1, -1, -1, -1, -1, 1 },

{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },

{ 1, 1, 1, 1, -1, -1, -1, -1, -1, -1 } };

char x1[] = { ‘+’, ‘-‘, ‘*’, ‘/’, ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{‘, ‘}’ };// 棧頂元素

char x2[] = { ‘+’, ‘-‘, ‘*’, ‘/’, ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{‘, ‘}’ };// 算術表達式中的元素

int k = 0, m, n = 0;

for (m = 0; m 10; m++) // 查找2個進行比較的運算符在表中的位置,並返回比較結果

{

for (n = 0; n 10; n++)

{

if (x1[m] == a x2[n] == b)

{

k = 1;

break; // 找到比較結果後,跳出循環

}

}

if (k == 1)

break;

}

return s[m][n];// 返回比較結果

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == jButton1)

{

show += “1”;

text.setText(show);

}

if (e.getSource() == jButton2)

{

show += “2”;

text.setText(show);

}

if (e.getSource() == jButton3)

{

show += “3”;

text.setText(show);

}

if (e.getSource() == jButton4)

{

show += “4”;

text.setText(show);

}

if (e.getSource() == jButton5)

{

show += “5”;

text.setText(show);

}

if (e.getSource() == jButton6)

{

show += “6”;

text.setText(show);

}

if (e.getSource() == jButton7)

{

show += “7”;

text.setText(show);

}

if (e.getSource() == jButton8)

{

show += “8”;

text.setText(show);

}

if (e.getSource() == jButton9)

{

show += “9”;

text.setText(show);

}

if (e.getSource() == jButton10)

{

show += “0”;

text.setText(show);

}

if (e.getSource() == jButton11)

{

show += “+”;

text.setText(show);

}

if (e.getSource() == jButton12)

{

show += “-“;

text.setText(show);

}

if (e.getSource() == jButton13)

{

show += “*”;

text.setText(show);

}

if (e.getSource() == jButton14)

{

show += “/”;

text.setText(show);

}

if (e.getSource() == jButton15)

{

show += “(“;

text.setText(show);

}

if (e.getSource() == jButton16)

{

show += “)”;

text.setText(show);

}

if (e.getSource() == jButton17)

{

show += “[“;

text.setText(show);

}

if (e.getSource() == jButton18)

{

show += “]”;

text.setText(show);

}

if (e.getSource() == jButton19)

{

show += “{“;

text.setText(show);

}

if (e.getSource() == jButton20)

{

show += “}”;

text.setText(show);

}

if (e.getSource() == jButton21)

{

show = “”;

text.setText(“”);

text1.setText(“”);

}

if (e.getSource() == jButton22)

{

show += “=”;

text.setText(show);

char str1[] = new char[50];

char str2[] = new char[50];

float result = 0;

str1 = show.toCharArray();

str2 = TranSmit(str1);

result = Count(str2);

text1.setText((new String(str2)).trim());

text.setText(“” + result);

show = “”;

}

}

private void initComponents()

{

text.setBounds(10, 10, 270, 30);

text1.setBounds(10, 50, 270, 30);

jButton1.setBounds(10, 90, 60, 25);

jButton2.setBounds(80, 90, 60, 25);

jButton3.setBounds(150, 90, 60, 25);

jButton4.setBounds(220, 90, 60, 25);

jButton5.setBounds(10, 120, 60, 25);

jButton6.setBounds(80, 120, 60, 25);

jButton7.setBounds(150, 120, 60, 25);

jButton8.setBounds(220, 120, 60, 25);

jButton9.setBounds(10, 150, 60, 25);

jButton10.setBounds(80, 150, 60, 25);

jButton11.setBounds(150, 150, 60, 25);

jButton12.setBounds(220, 150, 60, 25);

jButton13.setBounds(10, 180, 60, 25);

jButton14.setBounds(80, 180, 60, 25);

jButton15.setBounds(150, 180, 60, 25);

jButton16.setBounds(220, 180, 60, 25);

jButton17.setBounds(150, 210, 60, 25);

jButton18.setBounds(220, 210, 60, 25);

jButton19.setBounds(10, 210, 60, 25);

jButton20.setBounds(80, 210, 60, 25);

jButton21.setBounds(10, 240, 60, 25);

jButton22.setBounds(80, 240, 60, 25);

jButton1.setText(“1”);

jButton2.setText(“2”);

jButton3.setText(“3”);

jButton4.setText(“4”);

jButton5.setText(“5”);

jButton6.setText(“6”);

jButton7.setText(“7”);

jButton8.setText(“8”);

jButton9.setText(“9”);

jButton10.setText(“0”);

jButton11.setText(“+”);

jButton12.setText(“-“);

jButton13.setText(“*”);

jButton14.setText(“/”);

jButton15.setText(“(“);

jButton16.setText(“)”);

jButton17.setText(“[“);

jButton18.setText(“]”);

jButton19.setText(“{“);

jButton20.setText(“}”);

jButton21.setText(“CE”);

jButton22.setText(“=”);

jButton1.addActionListener(this);

jButton2.addActionListener(this);

jButton3.addActionListener(this);

jButton4.addActionListener(this);

jButton5.addActionListener(this);

jButton6.addActionListener(this);

jButton7.addActionListener(this);

jButton8.addActionListener(this);

jButton9.addActionListener(this);

jButton10.addActionListener(this);

jButton11.addActionListener(this);

jButton12.addActionListener(this);

jButton13.addActionListener(this);

jButton14.addActionListener(this);

jButton15.addActionListener(this);

jButton16.addActionListener(this);

jButton17.addActionListener(this);

jButton18.addActionListener(this);

jButton19.addActionListener(this);

jButton20.addActionListener(this);

jButton21.addActionListener(this);

jButton22.addActionListener(this);

add(text);

add(text1);

add(jButton1);

add(jButton2);

add(jButton3);

add(jButton4);

add(jButton5);

add(jButton6);

add(jButton7);

add(jButton8);

add(jButton9);

add(jButton10);

add(jButton11);

add(jButton12);

add(jButton13);

add(jButton14);

add(jButton15);

add(jButton16);

add(jButton17);

add(jButton18);

add(jButton19);

add(jButton20);

add(jButton21);

add(jButton22);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(null);

setBounds(300, 300, 300, 300);

setVisible(true);

}

public static void main(String args[])

{

new jisuanqi();

}

}

java實驗報告總結

源代碼:

public class Exe1_1 {

public static void main (String args[]){

byte a1=127;

int a2=12345;

long a3=123456789L;

float a4=1.23e-5f;

double a5=1.4567e3;

char a6=65;

boolean a7=true;

System.out.println(“a1=”+a1+”\na2=”+a2+”\na3=”+a3+”\na4=”+a4+”\na5=”+a5+”\na6=”+a6+”\na7=”+a7);

}

}運行結果:

2.編寫Java小應用程序,輸出兩行字元串:「Java很有趣。」和「努力學習Java編程。」,輸出的起點坐標是(20,20),行距是50像素。源程序代碼:

import java.awt.Graphics;

import java.applet.Applet;

public class Exe1_2 extends Applet{

public void paint(Graphics g){

g.drawString(“Java很有趣。”,20,20);

g.drawString(“努力學習Java編程。”,20,70);

}

}運行情況:

3.使用算術運算符得到一個4位十進位數的各位數字並輸出,然後輸出該數的逆序數和各位數字平方後相加的和。

源程序代碼:

public class Exe1_3 {

public static void main (String args[]){

int n=3756,a,b,c,d,n1;

a=n/1000;

b=(n-1000*a)/100;

d=n%10;

c=(n%100-d)/10;

System.out.println(“3756的逆序數為:”+d+” “+c+” “+b+” “+a);

System.out.print(“各位數字平方後相加的和為:”);

System.out.print(a*a+b*b+c*c+d*d);

}

}

運行結果:

三、實驗總結(是否完成實驗、實驗過程中的問題以及解決方法分析等)

本次是我第一次使用JAVA來進行編程,感覺很是不一樣,在前兩節課中我們學習了有關Java的一些簡單知識。然後這次的實驗是對最初的一種練習。

在第一題中,我們重要是認識java的運行環境,並且了解各種變數的數據類型。只要通過簡單的語句就可以通過運行。其中出現了一些單詞的拼寫錯誤,這些是可以修改正確的。第二題我們練習的十一個小的applet應用程序,需要設置一個網頁來顯示,這是一個很不一樣的地方,最後成功測試,裡面需要注意的是顯示位置的問題。第三題我們做的是一個有演算法要求的解決問題,在測試中出現了一點小問題,程序的演算法出了錯。還有一個問題是有關於顯示問題,最後的數據輸出需要用到兩行分別顯示字元和數據,這樣才不會出錯。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/294092.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-26 13:15
下一篇 2024-12-26 13:15

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29
  • Java 8 Group By 會影響排序嗎?

    是的,Java 8中的Group By會對排序產生影響。本文將從多個方面探討Group By對排序的影響。 一、Group By的概述 Group By是SQL中的一種常見操作,它…

    編程 2025-04-29

發表回復

登錄後才能評論