几个java小作业,JAVAweb大作业

发布时间:2022-11-17

本文目录一览:

  1. java小作业
  2. JAVA的几道题目
  3. 求解两道JAVA作业题!悬赏50,在线等!~
  4. 线程应用-java小作业
  5. JAVA作业,急求!!!
  6. 做一个简单的Java作业,大二期末作业

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!");
                }
            }
        }
    }
}

JAVA的几道题目

public class ThreeNumber {
    public static void main(String agrs[]) {
        int t = 0;
        for (int i = 100; i <= 1000; i++) {
            if (i % 3 == 0) {
                System.out.printf("%4d", i);
                t++;
                if (t % 6 == 0)
                    System.out.println(""); // 每行打印6个数
            }
        }
    }
}
public class CiFang {
    public static void main(String agrs[]) {
        System.out.println(find2(40));
    }
    static double find2(int n) {
        return Math.pow(2, n);
    }
}
public class ShowCar {
    public static void main(String[] args) {
        Car bentian = new Car("本田", "黑色", 1500, 5);
        System.out.println("品牌:" + bentian.name);
        System.out.println("颜色:" + bentian.color);
        System.out.println("自重:" + bentian.weight + "公斤");
        System.out.println("搭乘人数:" + bentian.passenger);
    }
}
class Car {
    String name;
    String color;
    double weight;
    int passenger;
    public Car(String name, String color, double weight, int passenger) {
        this.name = name;
        this.color = color;
        this.weight = weight;
        this.passenger = passenger;
    }
}
public class Array {
    public static void main(String[] args) {
        int[] number = {5, 6, 1, 7, 3};
        int min = number[0], max = number[0], sum = number[0];
        for (int i = 1; i < number.length; i++) {
            sum = sum + number[i];
            if (number[i] < min)
                min = number[i];
            if (number[i] > max)
                max = number[i];
        }
        System.out.println("数组元素和:" + sum);
        System.out.println("数组最小元素:" + min);
        System.out.println("数组最大元素:" + max);
    }
}
import java.io.*;
public class TestString {
    private String input;
    String[] strs = new String[10]; // 根据输入生成的字符串数组
    private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter stdOut = new PrintWriter(System.out, true);
    public void inputString() throws IOException {
        stdOut.println("请输入10个以内的字符串,输入exit结束输入:");
        int i = 0;
        while ((input = stdIn.readLine()) != null) {
            String[] temp = new String[i + 1];
            if (input.equals("exit"))
                break;
            strs[i] = input;
            i++;
        }
        for (int k = 0; k < i; k++)
            System.out.println(strs[k]);
    }
    public static void main(String[] args) {
        try {
            new Homework().inputString();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

最后一个程序可能不是很完善,最多只能输入十个字符串!

求解两道JAVA作业题!悬赏50,在线等!

第一题:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ex3_1 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        char c;
        int i = 0;
        String s = "The past is gone and static. Nothing we can do willchange it. "
                + "Thefuture is before us and dynamic. Everything we do will affect it.";
        System.out.println("请输入一个字母");
        Scanner sc = new Scanner(System.in);
        c = sc.next().charAt(0); // 输入单个字符
        Pattern p = Pattern.compile(String.valueOf(c));
        Matcher m = p.matcher(s);
        while (m.find()) {
            i++;
        }
        System.out.println("指定字符" + c + "出现的频率为" + i);
    }
}

第二题:

import java.util.Arrays;
public class Ex3_2 {
    public static void main(String[] args) {
        int[] a = new int[20];
        int b = 0;
        int max = 0;
        for (int i = 0; i < a.length; i++) {
            a[i] = (int) (Math.random() * 99);
        }
        System.out.println(Arrays.toString(a));
        for (int j = 0; j < a.length; j++) {
            for (int k = 0; k < j; k++) {
                if (a[j] < a[k]) {
                    a[k] = a[j];
                    a[k] = b;
                    b = a[j];
                }
            }
        }
        max = a[0];
        Arrays.sort(a);
        System.out.println(max);
    }
}

线程应用-java小作业

  1. 创建一个动物集合,插入动物园中有的几种动物(请给出10种)
  2. 一次性输出内容
  3. 使用iterator遍历集合中所有内容
  4. 将集合内容转存储于一个数字内,并在数组中进行排序 只列举了2种动物,自己再添加
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
/**
 *
 * @author Administrator
 */
interface Animal {
    public String toString();
}
class Cat implements Animal, Comparable<Animal> {
    public String name;
    public Cat() {
        super();
        name = "猫";
    }
    public String toString() {
        return name;
    }
    @Override
    public int compareTo(Animal o) {
        if (this.toString().compareTo(o.toString()) == -1) {
            return -1;
        } else if (this.toString().compareTo(o.toString()) == 0) {
            return 0;
        } else
            return 1;
    }
}
class Dog implements Animal, Comparable<Animal> {
    public String name;
    public Dog() {
        super();
        name = "狗";
    }
    public String toString() {
        return name;
    }
    @Override
    public int compareTo(Animal o) {
        if (this.toString().compareTo(o.toString()) == -1) {
            return -1;
        } else if (this.toString().compareTo(o.toString()) == 0) {
            return 0;
        } else
            return 1;
    }
}
public class Demo8 {
    public static void main(String[] args) {
        // 创建一个动物集合,插入动物园中有的几种动物
        Collection<Animal> col = new ArrayList<Animal>();
        col.add(new Cat());
        col.add(new Dog());
        col.add(new Cat());
        // 一次性输出内容
        System.out.println(Arrays.toString(col.toArray()));
        // 使用iterator遍历集合中所有内容
        // 并将集合内容转存储于一个数组内
        Iterator<Animal> it = col.iterator();
        int n = 0;
        Animal[] an = new Animal[3];
        while (it.hasNext()) {
            Animal temp = (Animal) it.next();
            System.out.println("使用iterator遍历集合中所有内容:" + temp);
            an[n++] = temp;
        }
        // 并在数组中进行排序
        Arrays.sort(an);
        System.out.println(Arrays.toString(an));
    }
}

JAVA作业,急求!!!

没分啊写起来倒是很简单

一、

public class CircleClass {
    private static final double PI = 3.1415926;
    private double r;
    public CircleClass(double r) {
        this.r = r;
    }
    public double area() {
        return PI * r * r;
    }
    public static void main(String[] args) {
        CircleClass circle1 = new CircleClass(3.5);
        System.out.println("circle1的面积:" + circle1.area());
        CircleClass circle2 = new CircleClass(5.0);
        System.out.println("circle2的面积:" + circle2.area());
    }
}

二、

abstract class Student {
    public String name;
    public int age;
    public String degree;
    public abstract String show();
}
class Undergraduate extends Student {
    private String specialty;
    public Undergraduate(String name, int age, String degree, String specialty) {
        this.name = name;
        this.age = age;
        this.degree = degree;
        this.specialty = specialty;
    }
    @Override
    public String show() {
        return name + ":" + age + "," + degree + "," + specialty;
    }
}
class Graduate extends Student {
    private String direction;
    public Graduate(String name, int age, String degree, String direction) {
        this.name = name;
        this.age = age;
        this.degree = degree;
        this.direction = direction;
    }
    @Override
    public String show() {
        return name + ":" + age + "," + degree + "," + direction;
    }
}
public class Test {
    public static void main(String[] args) {
        Student s1 = new Graduate("张三", 20, "本科", "通信");
        System.out.println(s1.show());
        Student s2 = new Graduate("李四", 21, "本科", "电子");
        System.out.println(s2.show());
        Student s3 = new Graduate("王五", 25, "硕士", "通信");
        System.out.println(s3.show());
        Student s4 = new Graduate("刘六", 36, "博士", "通信");
        System.out.println(s4.show());
    }
}

做一个简单的Java作业,大二期末作业

第一题直接上网搜java计算器,你想自己写也无所谓,不是多么难的事。第二题属于开放题类,你可以参考游戏中角色的属性值,比如说饱食度、血量之类的属性,六个知识点就更简单了,重载和抛异常就不说了,随便写一个继承类实现一个接口就能体现出重写、继承、接口。最后剩下一个你可以挑一个你比较熟悉的就行了,不建议选多线程,特别容易出问题,不管是正则还是文件流都是很容易体现的。