大数据预科班作业5、6
1. 写一个函数add,接受两个整数作为参数,返回这两个整数的和。
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
// 测试
System.out.println(add(1, 2));
}
public static int add(int x, int y) {
return (x + y);
}
}
2. 写一个函数,接受一个整数,输出这个整数的所有因子。
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
//测试
getSub(8);
}
public static void getSub(int x) {
for (int i = 1; i <= x; i++) {
if (x % i == 0) {
System.out.println(i);
}
}
}
}
3. 写一个函数,接受一个整数n,输出1+2+3+...+n 的和
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
//测试
System.out.println(add(5));
}
public static int add(int x) {
if (x == 1) {
return 1;
}
return (x + add(--x));
}
}
4. 写一个函数,接受一个整数参数n,输出n 个HelloWorld
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
printHelloWord(11);
}
public static void printHelloWord(int n) {
for (int i = 0; i < n; i++) {
System.out.println("Hello world!");
}
}
}
5. *写一个函数,接受一个整数,输出这个整数是几位数
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
System.out.println(getSum(1331));
}
public static int getSum(int n) {
int sum = 0;
while (n != 0) {
n /= 10;
sum++;
}
return sum;
}
}
6. *写一个函数,判断一个整数是否是质数
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
System.out.println(isZhiShu(7));
}
public static boolean isZhiShu(int n) {
boolean isZ = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isZ = false;
}
}
return isZ;
}
}
7. *写一个函数,计算两点(x1, y1)和(x2, y2)之间的距离
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
System.out.println(PointLength(1, 1, 2, 3));
}
public static double PointLength(int x1, int y1, int x2, int y2) {
return Math.sqrt(Math.pow(Math.abs(x1 - x2), 2)
+ Math.pow(Math.abs(y1 - y2), 2));
}
}
8. *写一个函数,接受三个整数a, b, c,计算ax2+bx+c=0 的根。
1. b方-4ac<0 无解
2. b方-4ac=0 相同两个解
3. b方-4ac>0 不同的两个解
解1:(-b+Math.sqrt(b方-4ac))/2a
解1:(-b-Math.sqrt(b方-4ac))/2a
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
getResult(1, 4, 4);
}
public static void getResult(int a, int b, int c) {
if ((Math.pow(b, 2) - 4 * a * c) < 0) {
System.out.println("无解");
} else {
System.out
.println("解1:"
+ ((-b + (Math.sqrt(Math.pow(b, 2) - 4 * a * c))) / (2 * a)));
System.out
.println("解2:"
+ ((-b - (Math.sqrt(Math.pow(b, 2) - 4 * a * c))) / (2 * a)));
}
}
}
9. *求一个三位数,该三位数等与其每位数字的阶乘之和。
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
System.out.println(getResult(23));
}
public static int getResult(int n) {
int sum = 0;
while (n != 0) {
sum += getJieCheng(n % 10);
n /= 10;
}
return sum;
}
public static int getJieCheng(int x) {
if (x == 1) {
return 1;
}
return x * getJieCheng(--x);
}
}
10. *验证角谷猜想:任给一个自然数,若为偶数除以2,若为奇数则乘3 加1,得到一个新的自然数后按照上面的法则继续计算,若干次后得到的结果必然为1。要求:读入一个自然数,输出计算的步骤。
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
jiaogu(8);
}
public static void jiaogu(int x) {
while (x != 1) {
if (x % 2 == 0) {
System.out.println(x + "/2=" + (x / 2));
x = x / 2;
} else {
System.out.println(x + "*3+1=" + (x * 3 + 1));
x = x * 3 + 1;
}
}
}
}
11. *已知两个完全平方三位数abc 和xyz,其中a、b、c、x、y、z 未必是不同的,而ax、by、cz 是三个完全平方数。求abc 和xyz
package com.peng.demo;
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
int arrNUm = 0;// 三位数完全平方数数组大小
int index = 100;
for (int i = 100; i <= 999; i++) {
if (isWQPFS(i)) {
arrNUm++;
}
}
// 创建数组
int[] threeArray = new int[arrNUm];
// 给数组赋值
for (int i = 0; i < arrNUm; i++) {
for (int j = index; j <= 999; j++) {
if (isWQPFS(j)) {
threeArray[i] = j;
index = ++j;
break;
}
}
}
// 循环(前后双层循环)三位完全平方数数组,并判断其组成的三个数是否为完全平方数
for (int i = 0; i < threeArray.length; i++) {
for (int j = 0; j < threeArray.length; j++) {
int[] temp = getGroupNum(threeArray[i], threeArray[j]);
if ((isWQPFS(temp[0])) && (isWQPFS(temp[1]))
&& (isWQPFS(temp[2]))) {
System.out.println(threeArray[i] + "和" + threeArray[j]);
}
}
}
}
public static boolean isWQPFS(int x) {
double temp = Math.sqrt(x);
int temp1 = (int) (temp);
if (temp - temp1 > 0) {
return false;
}
return true;
}
// 获取组装数
public static int[] getGroupNum(int three1, int three2) {
int[] temp = new int[3];
int three1_1 = 0;
int three1_2 = 0;
int three1_3 = 0;
int three2_1 = 0;
int three2_2 = 0;
int three2_3 = 0;
three1_1 = three1 % 10;
three1 /= 10;
three1_2 = three1 % 10;
three1 /= 10;
three1_3 = three1 % 10;
three2_1 = three2 % 10;
three2 /= 10;
three2_2 = three2 % 10;
three2 /= 10;
three2_3 = three2 % 10;
temp[0] = 10 * three1_1 + three2_1;
temp[1] = 10 * three1_2 + three2_2;
temp[2] = 10 * three1_3 + three2_3;
return temp;
}
}
12. *如果整数A 的全部因子(包括1,不包括A 本身)之和等于B,且整数B 的全部因子包括1,不包括B 本身)之和等于A,则称整数A\B 是一对亲密数。求3000 以内的全部亲密数。
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
// 亲密数:A的因数的和=B,且b的因数之和等于A
for (int i = 3; i <= 3000; i++) {
int tt = getXSum(i);
int mm = getXSum(tt);
if (i == mm && tt > i) {
System.out.println(i);
}
}
}
// 参数:数X
// 返回值:数X的因数之和
public static int getXSum(int x) {
int sum = 1;
for (int i = 2; i <= x / 2; i++) {
if (x % i == 0) {
sum += i;
}
}
return sum;
}
}
13. **验证哥德巴赫猜想:任何一个大于6 的偶数,都能分解成两个质数的和。要求输入一个整数,输出这个数能被分解成哪两个质数的和。eg : 14=3+11;14=7+7
package com.peng.demo;
/* * 验证哥德巴赫猜想:任何一个大于6 的偶数, * 都能分解成两个质数的和。要求输入一个整数,输出这个数能被分解成哪两个质数的和。eg : 14:14=3+11;14=7+7 */
public class Demo01 {
void Demo1() {
System.out.println("wwwwww");
}
public static void main(String[] args) {
for (int i = 7; i <= 100; i++) {
for (int j = 2; j < i; j++) {
if (isZhiShu(j)) {
if (isZhiShu(i - j) && (1 != i - j)) {
System.out.println(i + "=" + j + "+" + (i - j));
}
}
}
}
}
public static boolean isZhiShu(int x) {
boolean isZhiShu = true;
for (int i = 2; i <= x / 2; i++) {
if (x % i == 0) {
isZhiShu = false;
break;
}
}
return isZhiShu;
}
}
14. ***用递归的方法解决汉诺塔问题:汉诺塔是源自印度神话。上帝创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上安大小顺序摞着n 片黄金圆盘。上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。读入 n,输出移动的顺序。
package com.peng.demo;
public class Demo01 {
public static void main(String[] args) {
tower(3, 'A', 'B', 'C');
}
public static void tower(int n, char fromDisk, char midDisk, char toDisk) {
if (n == 1) {
System.out.println("1号盘子从" + fromDisk + "移动到" + toDisk);
return;
}
tower(n - 1, fromDisk, toDisk, midDisk);
System.out.println(n + "号盘子从" + fromDisk + "移动到" + toDisk);
tower(n - 1, midDisk, fromDisk, toDisk);
}
}
1. (重载,实例变量)有以下代码:
class ClassA{
public void method(int value){
System.out.println(value);
}
public void method(){
System.out.println(value);
}
int value;
}
class TestClassA{
public static void main(String args[]){
ClassA classA = new ClassA();
classA.value = 10;
classA.method();
classA.method(20);
}
}
请选择正确结果:
A. 编译不通过 B. 输出10 10 C. 输出 10 20 D. 输出0 20
C(10,20):测试通过
2. (方法重载,函数返回值)有以下代码
class ClassA{
void method(){
System.out.println("method()");
}
int method(int i){
System.out.println("method(int)");
}
public static void main(String args[]){
ClassA a = new ClassA();
a.method();
a.method(10);
}
}
该程序是否能编译通过?如果可以,写出该程序运行结果。如果不能,请说明理由,以及如何修
答:编译不通过,int method(int i){System.out.println("method(int)");}没有返回语句
可以改为:
int method(int i){
System.out.println("method(int)");
return 1;
}
3. (构造方法)关于构造方法,下列说法正确的是:
A. 每个类中都有至少一个构造方法
B. 一个类中可以有多个构造方法
C. 构造方法可以有返回值
D. 构造方法可以有多个参数
B,C,D
A错:类可以不写构造函数,JVM或默认添加一个无参构造函数
4. (引用)有以下代码
class MyClass{
int value;
}
public class TestRef{
public static void main(String args[]){
int a = 10;
int b = a;
b ++ ;
System.out.println(a);
MyClass mc1 = new MyClass();
mc1.value = 10;
MyClass mc2 = mc1;
mc2.value ++;
System.out.println(mc1.value);
}
}
请写出编译运行后的结果
10
11
亲测正确
5. (引用)有以下代码
class ClassA{
int value = 10;
}
public class TestReturnRef{
public static void main(String args[]){
ClassA ca = new ClassA();
ca = getObject();
ca = getObject();
ca = getObject();
System.out.println(ca.value);
}
public static ClassA getObject(){
ClassA newObject = new ClassA();
newObject.value += 10;
return newObject;
}
}
编译运行TestReturnRef 程序,结果为:
A. 编译出错 B. 输出10 C. 输出20 D. 输出40
C、20 亲测正确
6. (构造函数)有以下代码
class MyClass{
int value;
}
public class TestMyClass{
public static void main(String args[]){
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass(10);
System.out.println(mc1.value);
System.out.println(mc2.value);
}
}
问:这个程序能否编译通过?如果可以,输出结果是什么?如果不可以,则应该如何修改?
MyClass mc2 = new MyClass(10);//编译有错
修改之后:
package com.peng.demo;
class MyClass {
int value;
public MyClass() {
}
public MyClass(int value) {
this.value = value;
}
}
public class TestMyClass {
public static void main(String args[]) {
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass(10);
System.out.println(mc1.value);
System.out.println(mc2.value);
}
}
执行结果:
0
10
7. (面向对象基础)根据注释,把下面代码补充完整
//定义一个Dog 类
class Dog{
//定义一个name 属性,该属性为String 类型
_____String name;_______
//定义一个age 属性,该属性为int 类型
________int age ;_______
//定义一个sexual 属性,该属性为boolean 类型
//true 表示为公,false 表示为母
_______boolean sexual;_____
public Dog(){
}
public Dog(String name, int age, boolean sexual){
//分别根据参数,设置Dog 类的属性
_____this.name=name;_______
_____this.age=age;_______
_____this.sexual=sexual;_______
}
public void play(){
System.out.println(name + “ play”);
}
public void play(int n){
System.out.println(name + “ play ” + n + “ minutes”);
}
}
public class TestDog{
public static void main(String args[]){
Dog d;
//创建一个Dog 对象,利用带参数的构造函数
//名字为joy,年龄为2 岁,性别为母
________d=new Dog("joy",2,false);_________
//调用Dog 对象无参的play 方法。
_______s.play();__________
//调用Dog 对象有参的play 方法,参数为30
_____d.play(30);__________
}
}
注:答案在横线中间(————————————————————————)填写
8. *(对象创建过程)有以下代码
class ClassA{
public ClassA(){
System.out.println("ClassA()");
}
}
class ClassB{
public ClassB(){
System.out.println("ClassB()");
}
}
class ClassC{
ClassA a = new ClassA();
ClassB b;
public ClassC(){
System.out.println("ClassC()");
b = new ClassB();
}
}
public class TestConstructor{
public static void main(String args[]){
ClassC cc = new ClassC();
}
}
请选择正确答案:
A. 编译不通过
B. 输出ClassA() ClassB() ClassC()
C. 输出 ClassA() ClassC() ClassB()
D. 输出 ClassC() ClassB() ClassA()
B,亲测
9. *(引用,方法参数传递)有以下代码
class ClassA{
int value;
}
public class TestClassA{
public static void main(String args[]){
int value = 10;
changeInt(value);
System.out.println(value);
ClassA ca = new ClassA();
ca.value = 10;
changeObject(ca);
System.out.println(ca.value);
}
public static void changeInt(int value){
value++;
}
public static void changeObject(ClassA ca){
ca.value++;
}
}
编译运行TestClassA 时,结果是:
A. 编译出错 B. 输出 10 11 C. 输出 10 10 D. 输出 11 11
B,亲测
10. *(构造函数,this 关键字)程序改错
public class Student{
public void Student(){
}
void init(){
age = 10;
name = "limy";
}
public Student(String name){
this.init();
this.name = name;
}
public Student(String name, int age){
this.init();
this(name);
this.age = age;
}
int age;
String name;
}
package com.peng.demo;
public class Student {
public void Student() {
}
void init() {
age = 10;
name = "limy";
}
public Student(String name) {
this.init();
this.name = name;
}
public Student(String name, int age) {
this(name);/构造函数调构造函数放第一行
this.init();
this.age = age;
}
int age;
String name;
}
11. (面向对象基础)写一个Worker 类,并创建多个Worker 对象。
1) 为Worker 类添加三个属性
1)String 类型的name,表示工人的姓名;
2)int 类型的age,表示工人的年龄;
3)double 类型的salary,表示工人的工资。
2) 为Worker 类添加两个构造方法
1)公开无参构造方法;
2)接受三个参数的构造方法,三个参数分别为字符串、int 和double 类型。
3) 为Worker 类添加两个work 方法,一个无参,另一个带整数参数,表示工人工作的时间(为多少小时)。
类图如下:
Worker |
---|
name:String age:int salary:double |
Worker() Worker(name:String,age:int,salary:double) work() work(hours:int) |
package com.peng.demo;
public class Worker {
String name;
int age;
double salary;
public Worker() {
}
public Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public void work() {
}
public void work(int hours) {
}
}
12. (面向对象基础)创建一个Address 类,描述如下:1) 该类有两个属性,1)String 类型的address,表示地址;2)String 类型的zipCode,表示邮编。2) 该类有两个构造方法,一为无参构造方法,一为带三个参数的方法。类图如下:
Address |
---|
address:String zipCode:int |
Address() Address(address:String,zipCode:String) |
package com.peng.demo;
public class Address {
String address;
String zipCode;
public Address() {
}
public Address(String address, String zipCode) {
this.address = address;
this.zipCode = zipCode;
}
}
13. *(面向对象基础)为第1 题中的Worker 类添加一个属性addr,该属性为Address 类型。创建一个Worker 对象,其姓名为"zhangsan",年龄为25,工资为2500,家庭住址为“北京市海淀区清华园1 号”,邮政编码为100084
14. **(引用,方法参数传递)有以下代码
class ClassA{
int value;
}
public class ChangeRef{
public static void main(String args[]){
ClassA ca = new ClassA();
changeValue(ca);
System.out.println(ca.value);
changeRef(ca);
System.out.println(ca.value);
}
public static void changeValue(ClassA ca){
ca.value = 100;
}
public static void changeRef(ClassA ca){
ca = new ClassA();
ca.value = 200;
}
}
编译运行ChangeRef,结果为:
A. 编译不通过 B. 输出100 200 C. 输出100 100 D. 输出0 200
C,亲测
15. (面向对象基础)*复数概念如下:每个复数都有实部和虚部。例如,3 + 5i,3 为实部,5i 为虚部。其中,i 称为虚数单位,有ii = -1。两个复数进行加法运算,运算时实部与实部相加,虚部与虚部相加。
例如:(1.5 – 3i) + (2.3 + 2.4i) = (1.5+2.3) + (-3 + 2.4)i = 3.8 – 0.6i
两个复数进行减法运算,与加法运算类似。两个复数进行乘法运算,
其过程如下:(a+bi) * (c + di) = ac + adi + bci + bd(i*i) = (ac-bd) + (ad+bc)i
例如:(3+5i)*(4+6i)= (3*4-5*6)+(3*6+4*5)i = -18 + 38i写一个类Complex,
用来表示复数。这个复数类具有两个属性:double real,表示实部;double im,表示虚部。
并为Complex 类增加add、sub、mul 方法,分别表示复数的加法、减法和乘法运算。
其中,add 方法的声明如下:
public Complex add(Complex c) //表示当前Complex 对象与参数c 对象相加
public Complex add(double real) //表示当前 Complex 对象与实数 real 相加
package com.peng.demo;
public class Complex {
// 定义属性
double RealPart;
double ImagePart;
// 定义构造函数
public Complex() {
}
public Complex(double R, double I) {
this.RealPart = R;
this.ImagePart = I;
}
// 定义公有方法
void setReal(double r) {
this.RealPart = r;
}
void setImage(double i) {
this.ImagePart = i;
}
double getReal() {
return this.RealPart;
}
double getImage() {
return this.ImagePart;
}
Complex ComplexAdd(Complex a) { // 加
return new Complex(this.RealPart + a.RealPart, this.ImagePart
+ a.ImagePart);
}
Complex ComplexSub(Complex a) { // 减
return new Complex(this.RealPart - a.RealPart, this.ImagePart
- a.ImagePart);
}
Complex ComplexMulti(Complex a) { // 乘
double r = this.RealPart * a.RealPart - this.ImagePart * a.ImagePart;
double i = this.RealPart * a.ImagePart + this.ImagePart * a.RealPart;
return new Complex(r, i);
}
Complex ComplexDiv(Complex a) { // 除
if (this.RealPart == 0 && a.ImagePart == 0) { // 根据后面得到的内容
System.out.println("输入的被除数不能都为0!\n");
return null;
}
// 化简得到公式为:(a+bi)/(c+di)
// 实体部分:(a*c+b*d)/(a*a+d*d)
// 虚体部分:(b*c-a*d)/(a*a+d*d)
double xx = this.RealPart * this.RealPart + a.ImagePart * a.ImagePart;
double r = (this.RealPart * a.RealPart + this.ImagePart * a.ImagePart)
/ xx;
double i = (this.ImagePart * a.RealPart - this.RealPart * a.ImagePart)
/ xx;
return new Complex(r, i);
}
boolean isEqual(Complex a) {
return (this.RealPart == a.RealPart && this.ImagePart == a.ImagePart);
}
String ToString() {
return this.RealPart + "+" + this.ImagePart + "i";
}
}