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/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

发表回复

登录后才能评论