前言:实验2的最后一道题还没有写后续进行补充,写的如有错误望大家理解指正。
7187、
import java.util.Scanner; class Cube{int c,k,g;public void get(int x,int y,int z){c=x;k=y;g=z;}public int getBulk(){return c*k*g;} } public class Main {public static void main(String[] args) {Cube c=new Cube();Scanner in=new Scanner(System.in);int x,y,z;x=in.nextInt();y=in.nextInt();z=in.nextInt();int Bulk;c.get(x,y,z);Bulk=c.getBulk();System.out.println("体积="+Bulk);} }
7188、
import java.util.Scanner; class Student{private String name;private int age;public void setName(String name_){name=name_;}public void setAge(int age_){age=age_;}public void getName(){System.out.print("该生姓名:"+name+",");}public void getAge(){System.out.println("年龄:"+age);} } public class Main {public static void main(String[] args) {String name;int age;Scanner in=new Scanner(System.in);name=in.next();age=in.nextInt();Student s1=new Student();s1.setAge(age);s1.setName(name);s1.getName();s1.getAge();} }
7202、
import java.util.*; class Complex{private int a, b;Complex(int a_,int b_){a=a_;b=b_;}Complex(Complex t){a=t.a;b=t.b;}public Complex Add(Complex r) {a+=r.a;b+=r.b;return this;}public Complex Sub(Complex r) {a=a-r.a;b=b-r.b;return this;}public void Print(){if(a==0&&b==0){System.out.println(0); }else System.out.println(a+" "+b+"i");} } public class Main{public static void main(String[] args) {int m,n; //第一个数的实部和虚部int p,q; //第二个数的实部和虚部Scanner scanner =new Scanner(System.in);m=scanner.nextInt();n=scanner.nextInt();p=scanner.nextInt();q=scanner.nextInt();Complex t =new Complex(m,n);Complex s =new Complex(t);Complex r=new Complex(p,q);(s.Add(r)).Print();(t.Sub(r)).Print();} }// Write your own code
7190、
import java.util.Scanner;class Add{ // private int a0,n,d;public static int Sum(int a0,int n,int d){int sum=0;int an=a0+(n-1)*d;sum=(n*(a0+an))/2;return sum;} } public class Main {public static void main(String[] args) {int a0,n,d;Scanner in=new Scanner(System.in);a0=in.nextInt();n=in.nextInt();d=in.nextInt();int sum;sum=Add.Sum(a0,n,d);System.out.println("和="+sum);} }
7203、
import java.util.Scanner;class ObjectCounter {static int sum=0;ObjectCounter(){sum++;}public static int getCount(){return sum;}//write your code here}public class Main{public static void main(String[] args) {Scanner scan = new Scanner(System.in);int n = scan.nextInt();for(int i=0;i<n;i++){ObjectCounter str1 = new ObjectCounter();}System.out.println(ObjectCounter.getCount());} }
7201、
import java.util.Scanner; class Point {private int x;private int y;public Point(int xx,int yy) //构造方法{this.x=xx;this.y=yy;}public int getX() {return x;}public int getY() {return y;}//write your codepublic static void fun1(Point p){System.out.println(p.getX());}public static Point fun2(){Point A =new Point(1,2);return A;}} public class Main{public static void main(String[] args) {//write your codeint x,y;Scanner in =new Scanner(System.in);x=in.nextInt();y=in.nextInt();Point A=new Point(x,y);Point B=new Point(A.getX(), A.getY());System.out.println(B.getX());Point.fun1(B);B=Point.fun2();System.out.println(B.getX());}}
7198、
import java.util.Scanner; class Dog{private String name,color;private int age;Dog(){name="dog1";color="white";age=1;}Dog(String name_,String color_,int age_){name=name_;color=color_;age=age_;}public String getName(){return name;}public String getcolor(){return color;}public int getAge(){return age;} }public class Main{public static void main(String[] args) {//write code hereString name,color;int age;Scanner in =new Scanner(System.in);name=in.next();color=in.next();age=in.nextInt();Dog d1=new Dog();Dog d2=new Dog(name,color,age);System.out.println("name=" + d1.getName() + ",color=" +d1.getcolor()+",age="+d1.getAge());System.out.println("name=" + d2.getName() + ",color=" +d2.getcolor()+",age="+d2.getAge());}}
7200、
import java.util.Scanner;class Check {//write your code hereCheck(int a ,int b){if(a<b){a=b;}System.out.println("Larger value: "+a);}Check(double x ,double y,double z){System.out.println("a*b*c = "+x*y*z);}Check(String s1,String s2){boolean flag= s1.equals(s2);System.out.println("Are equal: "+flag);} }public class Main {public static void main(String args[]) {//write your code hereint a,b;double x,y,z;String s1,s2;Scanner in =new Scanner(System.in);a=in.nextInt();b=in.nextInt();x=in.nextDouble();y=in.nextDouble();z=in.nextDouble();s1=in.next();s2=in.next();Check c1=new Check(a,b);Check c2=new Check(x,y,z);Check c3=new Check(s1,s2);} }
7199、
import java.util.Scanner; class Prime{// Write your own codepublic boolean prime(int n){if(n==1){return false;}else{for(int i=2;i<=n/2;i++){if(n%i==0){return false;}}}return true;} } public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();// Write your own codePrime p1=new Prime();if(p1.prime(n)==false){System.out.println("NO");}else{System.out.println("YES");}} }