Java基础练习题2
1.编写一个类实现复数运算,至少包含复数加减乘除。 2.随机输出1到100之间的10个数。。 3.利用ArrayList类创建一个对象,并向其添加若干个学生信息(包括但不限于学号、姓名、年龄、JAVA课程成绩等),然后随机选一个元素输出。 4.集合A={1,2,3,4}和集合B={1,3,5,7,9,11},编程求A与B的交集、并集和差集。 5.输入一个字符串输出它的子集。 6.输入两个字符串,检验第一个是否是第二个的字符串的子串。 答案: 1.编写一个类实现复数运算,至少包含复数加减乘除。
package lianxi1;
public class lianxi1 {
public static void main(String[] args) {
Complex c1=new Complex(1,1);
Complex c2=new Complex(2,2);
Complex c3=c1.jiafa(c2);
System.out.println("两个复数相加:");
c3.show();
Complex c4=c1.jianfa(c2);
System.out.println("两个复数相减:");
c4.show();
Complex c5=c1.chengfa(c2);
System.out.println("两个复数相乘:");
c5.show();
Complex c6=c1.chufa(c2);
System.out.println("两个复数相除:");
c6.show();
}
}
class Complex
{
private double real;
private double image;
public Complex(double real,double image)
{
this.real=real;
this.image=image;
}
public double getReal()
{
return real;
}
public void setReal(double real)
{
this.real=real;
}
public double getImage()
{
return image;
}
public void setImage(double image)
{
this.image=image;
}
public Complex jiafa(Complex fushu)
{
double shibu,xubu;
shibu=this.real+fushu.real;
xubu=this.image+fushu.image;
return new Complex(shibu,xubu);
}
public Complex jianfa(Complex fushu)
{
double shibu,xubu;
shibu=this.real-fushu.real;
xubu=this.image-fushu.image;
return new Complex(shibu,xubu);
}
public Complex chengfa(Complex fushu)
{
double shibu,xubu;
shibu=(this.real*fushu.real)-(this.image*fushu.image);
xubu=this.real*fushu.image+this.image*fushu.real;
return new Complex(shibu,xubu);
}
public Complex chufa(Complex fushu)
{
double shibu,xubu;
shibu=(this.real*fushu.real+this.image*fushu.image)/(fushu.real*fushu.real+fushu.image*fushu.image);
xubu=(this.image*fushu.real-this.real*fushu.image)/(fushu.real*fushu.real+fushu.image*fushu.image);
return new Complex(shibu,xubu);
}
public void show(){
if(image>0){
System.out.println(this.real+"+"+this.image+"i");
}
else if(image<0){
System.out.println(this.real+""+image+"i");
}
else {
System.out.println(real);
}
}
}
2.随机输出1到100之间的10个数。
package lianxi1;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
public class lianxi2 {
public static void main(String[] args) {
TreeSet<Integer> list = new TreeSet<>();
int n=0;
for (int i = 0; i < 10; i++)
{
Random r = new Random();
int a = r.nextInt(100);
list.add(a);
}
Iterator i = list.iterator();
while (i.hasNext())
{
System.out.println(i.next());
}
}
}
3.利用ArrayList类创建一个对象,并向其添加若干个学生信息(包括但不限于学号、姓名、年龄、JAVA课程成绩等),然后随机选一个元素输出。
package lianxi1;
import java.util.ArrayList;
import java.util.Random;
public class lianxi3 {
public static void main(String[] args) {
ArrayList list=new ArrayList();
Student1 stu1=new Student1(01,"张三",20,99);
Student1 stu2=new Student1(02,"李四",21,69);
Student1 stu3=new Student1(03,"小红",20,77);
Student1 stu4=new Student1(04,"小明",19,83);
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
System.out.println("输入的数据如下:\n");
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
System.out.println("随机输出一个元素:\n");
System.out.println("学号\t姓名\t年龄\t成绩\t");
Random r= new Random();
int a= r.nextInt(3);
System.out.println(list.get(a).toString());
}
}
class deldect{
private int id;
public deldect(int id) {
super();
this.id = id;
}
}
class Student1
{
private int id;
private String name;
private int age;
private int chengji;
public Student1(){}
public Student1(int id, String name, int age, int chengji) {
super();
this.id = id;
this.name = name;
this.age = age;
this.chengji = chengji;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getChenji() {
return chengji;
}
public void setChenji(int chenji) {
this.chengji = chenji;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", chengji=" + chengji + "]";
}
}
4…集合A={1,2,3,4}和集合B={1,3,5,7,9,11},编程求A与B的交集、并集和差集。
package lianxi1;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class liaxi4{
public static void main(String[] args)
{
Set list1=new HashSet();
Set list2=new HashSet();
Scanner reader=new Scanner(System.in);
System.out.println("输入A的元素个数:");
int a=reader.nextInt();
System.out.println("输入B的元素个数:");
int b=reader.nextInt();
System.out.println("输入A的元素:");
for(int i=0;i<a;i++)
{
int a1=reader.nextInt();
list1.add(a1);
}
System.out.println("输入B元素:");
for(int i=0;i<b;i++)
{
int b1=reader.nextInt();
list2.add(b1);
}
List<Integer> jiaoji=new ArrayList<Integer>(list1);
System.out.print("集合A与集合B的交集:\n");
jiaoji.retainAll(list2);
System.out.println(jiaoji);
List<Integer> bingji=new ArrayList<Integer>(list1);
bingji.removeAll(list2);
System.out.print("集合A与集合B的并集:\n");
bingji.addAll(list2);
System.out.println(bingji);
List<Integer> chaji1=new ArrayList<Integer>(list1);
System.out.print("集合A与集合B的差集:\n");
chaji1.removeAll(list2);
System.out.println(chaji1);
List<Integer> chaji2=new ArrayList<Integer>(list2);
System.out.print("集合B与集合A的交集:\n");
chaji2.removeAll(list1);
System.out.println(chaji2);
}
}
5.输入一个字符串输出它的子集、
package lianxi1;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class lianxi5 {
public static void main(String[] args) {
String data;
Scanner re=new Scanner(System.in);
System.out.println("输入要输入的字符串:\n");
data=re.next();
System.out.println("字符串子集为:"+getChildren(data));
}
public static List<String> getChildren(String data) {
Set<String> set = new TreeSet<String>();
for (int i = 1; i < data.length(); i++) {
for (int j = 0; j < data.length(); j++) {
if (j + i <= data.length()) {
set.add(data.substring(j, j + i));
}
}
}
List<String> list = new ArrayList<String>();
list.addAll(set);
return list;
}
}
6.输入两个字符串,检验第一个是否是第二个的字符串的子串。
package lianxi1;
import java.util.Scanner;
public class lianxi6 {
public static void main(String[] args) {
Scanner re = new Scanner(System.in);
System.out.println("Enter string s1: ");
String s1 = re.next();
System.out.println("Enter string s2: ");
String s2 = re.next();
boolean bool = s1.contains(s2);
if(bool)
System.out.println(s2 + "是" + s1+"子串");
else
System.out.println(s2 + "不是" + s1+"子串");
}
}
|