您的位置:

java题目,java题目大全

本文目录一览:

java 小题目

package util;

import java.util.Collection;

import java.util.Comparator;

import java.util.Iterator;

import java.util.List;

import java.util.ListIterator;

import java.util.Map;

import java.util.Set;

import java.util.SortedMap;

import java.util.SortedSet;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**

* p

* 操作集合的类,可以返回以读写锁实现的各类集合br

* /p

*

*/

public class CollectionUtils {

private CollectionUtils() {}

/**

* p

* 返回用读写锁实现的集合

* /p

*

* @param collection

* 基础集合

* @return 同步集合

*/

public static T CollectionT readWriteLockCollection(

CollectionT collection) {

return new ReadWriteLockCollectionT(collection);

}

/**

* p

* 返回用读写锁实现的List

* /p

*

* @param list

* 基础List

* @return 同步List

*/

public static T ListT readWriteLockList(ListT list) {

return new ReadWriteLockListT(list);

}

/**

* p

* 返回用读写锁实现的Set

* /p

*

* @param set

* 基础Set

* @return 同步Set

*/

public static T SetT readWriteLockSet(SetT set) {

return new ReadWriteLockSetT(set);

}

/**

* p

* 返回用读写锁实现的SortedSet

* /p

*

* @param sortedSet

* 基础SortedSet

* @return 同步SortedSet

*/

public static T SortedSetT readWriteLockSortedSet(SortedSetT sortedSet) {

return new ReadWriteLockSortedSetT(sortedSet);

}

/**

* p

* 返回用读写锁实现的Map

* /p

*

* @param map

* 基础Map

* @return 同步Map

*/

public static K, V MapK, V readWriteLockMap(MapK, V map) {

return new ReadWriteLockMapK, V(map);

}

/**

* p

* 返回用读写锁实现的SortedMap

* /p

*

* @param sortedMap

* 基础SortedMap

* @return 同步SortedMap

*/

public static K, V SortedMapK, V readWriteLockSortedMap(

SortedMapK, V sortedMap) {

return new ReadWriteLockSortedMapK, V(sortedMap);

}

private static class ReadWriteLockCollectionT implements CollectionT {

private CollectionT collection;

protected ReadWriteLock lock = new ReentrantReadWriteLock();

private ReadWriteLockCollection(CollectionT collection) {

if (collection == null)

throw new NullPointerException();

this.collection = collection;

}

@Override

public boolean add(T e) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return collection.add(e);

} finally {

lock.unlock();

}

}

@Override

public boolean addAll(Collection? extends T c) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return collection.addAll(c);

} finally {

lock.unlock();

}

}

@Override

public void clear() {

Lock lock = this.lock.writeLock();

lock.lock();

try {

collection.clear();

} finally {

lock.unlock();

}

}

@Override

public boolean contains(Object o) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return collection.contains(o);

} finally {

lock.unlock();

}

}

@Override

public boolean containsAll(Collection? c) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return collection.containsAll(c);

} finally {

lock.unlock();

}

}

@Override

public boolean isEmpty() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return collection.isEmpty();

} finally {

lock.unlock();

}

}

@Override

public IteratorT iterator() {

return collection.iterator();

}

@Override

public boolean remove(Object o) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return collection.remove(o);

} finally {

lock.unlock();

}

}

@Override

public boolean removeAll(Collection? c) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return collection.removeAll(c);

} finally {

lock.unlock();

}

}

@Override

public boolean retainAll(Collection? c) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return collection.retainAll(c);

} finally {

lock.unlock();

}

}

@Override

public int size() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return collection.size();

} finally {

lock.unlock();

}

}

@Override

public Object[] toArray() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return collection.toArray();

} finally {

lock.unlock();

}

}

@Override

public E E[] toArray(E[] a) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return collection.toArray(a);

} finally {

lock.unlock();

}

}

}

private static class ReadWriteLockListT extends

ReadWriteLockCollectionT implements ListT {

private ListT list;

private ReadWriteLockList(ListT list) {

super(list);

this.list = list;

}

@Override

public void add(int index, T element) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

list.add(index, element);

} finally {

lock.unlock();

}

}

@Override

public boolean addAll(int index, Collection? extends T c) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return list.addAll(index, c);

} finally {

lock.unlock();

}

}

@Override

public T get(int index) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return list.get(index);

} finally {

lock.unlock();

}

}

@Override

public int indexOf(Object o) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return list.indexOf(o);

} finally {

lock.unlock();

}

}

@Override

public int lastIndexOf(Object o) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return list.lastIndexOf(o);

} finally {

lock.unlock();

}

}

@Override

public ListIteratorT listIterator() {

return list.listIterator();

}

@Override

public ListIteratorT listIterator(int index) {

return list.listIterator(index);

}

@Override

public T remove(int index) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return list.remove(index);

} finally {

lock.unlock();

}

}

@Override

public T set(int index, T element) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return list.set(index, element);

} finally {

lock.unlock();

}

}

@Override

public ListT subList(int fromIndex, int toIndex) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockListT(

list.subList(fromIndex, toIndex));

} finally {

lock.unlock();

}

}

}

private static class ReadWriteLockSetT extends ReadWriteLockCollectionT

implements SetT {

private ReadWriteLockSet(SetT set) {

super(set);

}

}

private static class ReadWriteLockSortedSetT extends ReadWriteLockSetT

implements SortedSetT {

private SortedSetT sortedSet;

private ReadWriteLockSortedSet(SortedSetT sortedSet) {

super(sortedSet);

this.sortedSet = sortedSet;

}

@Override

public Comparator? super T comparator() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return sortedSet.comparator();

} finally {

lock.unlock();

}

}

@Override

public T first() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return sortedSet.first();

} finally {

lock.unlock();

}

}

@Override

public SortedSetT headSet(T toElement) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockSortedSetT(

sortedSet.headSet(toElement));

} finally {

lock.unlock();

}

}

@Override

public T last() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return sortedSet.last();

} finally {

lock.unlock();

}

}

@Override

public SortedSetT subSet(T fromElement, T toElement) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockSortedSetT(sortedSet.subSet(

fromElement, toElement));

} finally {

lock.unlock();

}

}

@Override

public SortedSetT tailSet(T fromElement) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockSortedSetT(

sortedSet.tailSet(fromElement));

} finally {

lock.unlock();

}

}

}

private static class ReadWriteLockMapK, V implements MapK, V {

private MapK, V map;

protected ReadWriteLock lock = new ReentrantReadWriteLock();

private ReadWriteLockMap(MapK, V map) {

if (map == null)

throw new NullPointerException();

this.map = map;

}

@Override

public void clear() {

Lock lock = this.lock.writeLock();

lock.lock();

try {

map.clear();

} finally {

lock.unlock();

}

}

@Override

public boolean containsKey(Object key) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return map.containsKey(key);

} finally {

lock.unlock();

}

}

@Override

public boolean containsValue(Object value) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return map.containsValue(value);

} finally {

lock.unlock();

}

}

@Override

public Setjava.util.Map.EntryK, V entrySet() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockSetjava.util.Map.EntryK, V(

map.entrySet());

} finally {

lock.unlock();

}

}

@Override

public V get(Object key) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return map.get(key);

} finally {

lock.unlock();

}

}

@Override

public boolean isEmpty() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return map.isEmpty();

} finally {

lock.unlock();

}

}

@Override

public SetK keySet() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockSetK(map.keySet());

} finally {

lock.unlock();

}

}

@Override

public V put(K key, V value) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return map.put(key, value);

} finally {

lock.unlock();

}

}

@Override

public void putAll(Map? extends K, ? extends V m) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

map.putAll(m);

} finally {

lock.unlock();

}

}

@Override

public V remove(Object key) {

Lock lock = this.lock.writeLock();

lock.lock();

try {

return map.remove(key);

} finally {

lock.unlock();

}

}

@Override

public int size() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return map.size();

} finally {

lock.unlock();

}

}

@Override

public CollectionV values() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockCollectionV(map.values());

} finally {

lock.unlock();

}

}

}

private static class ReadWriteLockSortedMapK, V extends

ReadWriteLockMapK, V implements SortedMapK, V {

private SortedMapK, V sortedMap;

private ReadWriteLockSortedMap(SortedMapK, V sortedMap) {

super(sortedMap);

this.sortedMap = sortedMap;

}

@Override

public Comparator? super K comparator() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return sortedMap.comparator();

} finally {

lock.unlock();

}

}

@Override

public K firstKey() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return sortedMap.firstKey();

} finally {

lock.unlock();

}

}

@Override

public SortedMapK, V headMap(K toKey) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockSortedMapK, V(

sortedMap.headMap(toKey));

} finally {

lock.unlock();

}

}

@Override

public K lastKey() {

Lock lock = this.lock.readLock();

lock.lock();

try {

return sortedMap.lastKey();

} finally {

lock.unlock();

}

}

@Override

public SortedMapK, V subMap(K fromKey, K toKey) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockSortedMapK, V(sortedMap.subMap(

fromKey, toKey));

} finally {

lock.unlock();

}

}

@Override

public SortedMapK, V tailMap(K fromKey) {

Lock lock = this.lock.readLock();

lock.lock();

try {

return new ReadWriteLockSortedMapK, V(

sortedMap.tailMap(fromKey));

} finally {

lock.unlock();

}

}

}

}

java编程题目

这不都说的很清楚了么。。。。。。。。

自己写吧,也没啥难度。

是完全不知道这个题目再说什么么?

package spring5.source;

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class D extends JFrame {

public static void main(String[] args) {

D d = new D();

d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

d.setSize(500, 500);

d.setLayout(new FlowLayout());

TextField t1 = new TextField();

TextField t2 = new TextField();

Button b = new Button("OK");

b.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String v1 = t1.getText();

try {

int n = Integer.parseInt(v1);

Double d = Math.pow(n, 2);

t2.setText(String.valueOf(d.intValue()));

} catch (Exception e2) {

e2.printStackTrace();

}

}

});

t1.addKeyListener(new KeyListener() {

@Override

public void keyTyped(KeyEvent e) {

}

@Override

public void keyReleased(KeyEvent e) {

}

@Override

public void keyPressed(KeyEvent e) {

if (e.getKeyChar() == KeyEvent.VK_ENTER) {

String v1 = t1.getText();

try {

int n = Integer.parseInt(v1);

Double d = Math.pow(n, 2);

t2.setText(String.valueOf(d.intValue()));

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

});

// KeyListener key_Listener = ;

d.add(t1);

d.add(t2);

d.add(b);

d.setVisible(true);

}

}

少了一个 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 关闭窗口的

java题目急求

Teacher.java

public class Teacher {

public double add(double a,double b) {

return a + b;

}

public double sub(double a,double b) {

return a - b;

}

}

Student.java

public class Student {

public void speak() {

System.out.println("老师好");

}

}

Java题目求解

你的程序有一处错误,f2=f3;应该是f1=f3;我帮你改过来了

方框里的f3=f1+f2;因为斐波那契数列是后一个数是前面两个数的和,所以这是求下一个斐波那契数f2=f1;和f1=f3;是准备下一个斐波那契数的前两项f1和f2

这么操作是因为判断一个数是否是斐波那契数,需要计算它前面的每一个斐波那契数,我们这样不断的求斐波那契数,

然后,赋新值给f2和f1,求下一个斐波那契数,直到求出的斐波那契数等于输入的数,则输入的数是斐波那契数,

或者大于输入的数,则输入的数不是斐波那契数.

完整的Java程序如下(改动的地方见注释)

import java.util.Scanner;

public class test11{

public static void main(String[] args){

int f1=1,f2=1,f3;

Scanner in=new Scanner(System.in);

System.out.println("请输入一个整数");

int number=in.nextInt();

while(numberf2){

f3=f1+f2;

f2=f1;

f1=f3;//这里把f2=f3;改成f1=f3;

}

if(number==f2){

System.out.println("是斐波那契数");

}else{

System.out.println("不是斐波那契数");

}

}

}

有关java编程题目?

按照题目要求编写的圆,圆锥和测试类的Java程序如下

Test.java文件内容如下

class Circle{

private double r;

private String color;

public Circle(double r){

this.r=r;

}

public double area(){

return Math.PI*r*r;

}

public double perimeter(){

return Math.PI*2*r;

}

public double getR(){

return this.r;

}

public void setR(double r){

this.r=r;

}

public String getColor(){

return this.color;

}

public void setColor(String color){

this.color=color;

}

public String toString(){

return "圆的半径为"+r+",颜色为"+color;

}

}

class Cone{

private Circle c;

private double h;

private String color;

public Cone(Circle c,double h){

this.c=c;

this.h=h;

}

public double volume(){

return 1.0/3*c.area()*h;

}

public Circle getCircle(){

return this.c;

}

public void setCircle(Circle c){

this.c=c;

}

public double getH(){

return this.h;

}

public void setH(double h){

this.h=h;

}

public String getColor(){

return this.color;

}

public void setColor(String color){

this.color=color;

}

public String toString(){

return "圆锥的底面积为"+c.area()+",高为"+h+",颜色为"+color;

}

}

public class Test{

public static void main(String[] args){

Circle circle1=new Circle(2.5);

circle1.setColor("红色");

System.out.println(circle1.toString());

System.out.println("圆的面积为"+circle1.area());

System.out.println("圆的周长为"+circle1.perimeter());

Cone circlar1=new Cone(circle1,2.7);

circlar1.setColor("蓝色");

System.out.println(circlar1.toString());

System.out.println("圆锥的体积为"+circlar1.volume());

}

}

java基础题目

/**

* Created by ${琳仔} on 2019/10/16.

*/

public class Person {

//身份证号码

private String idCard;

//姓名

private String name;

//性别

private int sex;

//年龄

private int age;

//籍贯

private String nativePlace;

//住址

private String address;

/**

* 吃什么食物

* @param food

*/

private void eating(String food){

System.out.println("吃了:"+food);

}

/**

* 购物

* @param goods

*/

private void shopping(String goods){

System.out.println("购物:"+goods);

}

/**

* 睡觉

* @param time

*/

private void sleeping(int time){

System.out.println("睡了:"+time);

}

/**

* 修改人的姓名

* @param aName

*/

private void setName(String aName){

this.name = aName;

System.out.println("修改姓名:"+aName);

}

/**

* 修改人的年龄

* @param aAge

*/

private void setAge(int aAge){

this.age = aAge;

System.out.println("修改年龄:"+aAge);

}

/**

* 打印人的所有信息

*/

public void printPerson() {

System.out.println("Person{" +

"idCard='" + idCard + '\'' +

", name='" + name + '\'' +

", sex=" + sex +

", age=" + age +

", nativePlace='" + nativePlace + '\'' +

", address='" + address + '\'' +

'}');

}

}