首页 > 编程语言 >JAVA 实 验

JAVA 实 验

时间:2024-11-29 20:35:34浏览次数:13  
标签:JAVA String int age System public name

import java.util.Scanner;
class Person
{
    private String name;
    private int age;
    private boolean gender;
    private int 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 boolean getgender(){
        return gender;
    }
    public void setgender(boolean gender){
        this.gender=gender;
    }
    public int getid(){
        return id;
    }
    public void setid(int id){
        this.id=id;
    }
        public Person()
    {
        System.out.println("This is constructor");
        //System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]");
        System.out.println(name+","+age+","+gender+","+id);
        //System.out.println("Person [name=null, age=0, gender=false, id=0]");
    }
    public Person(String name ,int age, boolean gender)
    {
        this.name=name;
        this.age=age;
        this.gender=gender;
    }
    public String toString()
    {
        return "Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]";
    }
}

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        Person[] p=new Person[n];
        for(int i=0;i<n;i++){
            String a=sc.next();
            int b=sc.nextInt();
            boolean c=sc.nextBoolean();
            p[i]=new Person(a,b,c);
        }
        for(int i=n-1;i>=0;i--)
        {
            System.out.println(p[i].toString());
        }
        Person pp=new Person();
        System.out.println(pp.toString());
    }
}
//----------------------------------------------------------------------------------------------------------------------------
import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
            Rectangle[] rectangle=new Rectangle[2];
            rectangle[0]=new Rectangle(in.nextInt(),in.nextInt());
            rectangle[1]=new Rectangle(in.nextInt(),in.nextInt());
            Circle[] circles=new Circle[2];
            circles[0]=new Circle(in.nextInt());
            circles[1]=new Circle(in.nextInt());
            System.out.println(rectangle[0].getPerimeter()+rectangle[1].getPerimeter()+
                    circles[0].getPerimeter()+circles[1].getPerimeter());
            System.out.println(rectangle[0].getArea()+rectangle[1].getArea()+
                    circles[0].getArea()+circles[1].getArea());
            System.out.println(Arrays.deepToString(rectangle));
            System.out.println(Arrays.deepToString(circles));
    }
 
}
class Rectangle{
    private int width;
    private int length;
    public Rectangle (int width,int length){
        this.width=width;
        this.length=length;
    }
    public int getPerimeter(){
        return (width+length)*2;
    }
    public int getArea(){
        return width*length;
    }
    public String toString(){
        return "Rectangle [" + "width=" + width + ", length=" + length + ']';
    }
}
class Circle{
    private int radius;
    private double s;
    public Circle(int radius){
        this.radius=radius;
    }
    public int getPerimeter() {
        s =  (Math.PI * 2 * radius);
        return (int)s;
    }
 
    public int getArea(){
        s = (Math.PI * radius * radius);
        return (int) s;
    }
    public String toString() {
        return "Circle [" + "radius=" + radius + ']';
    }
}
//-------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {   
        Scanner sc = new Scanner(System.in);
        String name=sc.next();
        int age=sc.nextInt();
        String sex=sc.next();
        Student s=new Student(name,sex,age);
        System.out.println(s.toString());
    }
}
class Student
{
    private String name;
    private String sex;
    private int age;
    public Student(String name ,String sex,int age){
        this.name =name;
        this.sex=sex;
        this.age=age;
    }
    public String toString(){
        return "Student [name='" +name+ "', sex='" +sex+ "', age=" +age+ "]";
    }
}
//----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        int c=sc.nextInt();
        int d=sc.nextInt();
        int e=sc.nextInt();
        System.out.println(a);
        System.out.println(b+c);
        System.out.println(d*e);
    }
}
//---------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Goods
{
    private String name;
    private double price;
    public Goods(String name,double price)
    {
        this.name=name;
        this.price=price;
    }
    public double getPrice(){
        return price;
    }
    public String toString(){
        return name+","+price;
    }
}
class Main{
    public static void main(String args[]){
        Goods ga[] =new Goods[5];
        Scanner sc = new Scanner(System.in);
        
        for(int i =0;i<5;i++){
            ga[i]= new Goods(sc.next(),sc.nextDouble());
        }        
        
        double  shouldPay = 0;
        for(Goods g:ga){
            shouldPay += g.getPrice();
            System.out.println(g.toString());
        }
        
        System.out.println("should pay:"+shouldPay);            
    }
}
//--------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Student
{
    private String name;
    private String gender;
    private int score;
    private boolean award;
    public Student(String name,String gender,int score,boolean award)
    {
        this.name=name;
        this.gender=gender;
        this.score=score;
        this.award=award;
    }
    //public 
    public String toString(){
        return "Student "+"[name="+name+", gender="+gender+", score="+score+", award="+award+"]";
    }
}
class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String name=sc.next();
        String gender=sc.next();
        int score=sc.nextInt();
        boolean award=sc.nextBoolean();
        Student st=new Student(name,gender,score,award);
        System.out.println(st.toString());
    }
}
//-----------------------------------------------------------------------------------------------
import java.util.Scanner;
class Main{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        Teacher[] t = new Teacher[2];
        int a=0;
        int b=0;
        for(int i=0;i<2;i++){
            int no=sc.nextInt();
            String name = sc.next();
            int age = sc.nextInt();
            String se=sc.next();
            t[i]=new Teacher(no,name,age,se);
            if(i==0) a=no;
            else b=no;
        }
        for(int i=0;i<2;i++)
        {
            System.out.println(t[i].toString());
        }
        if(a==b) System.out.printf("true");
        else System.out.printf("false");
    }
}
class Teacher{
    private int no;
    private String name;
    private int age;
    private String se;
    public Teacher(int no,String name ,int age,String se)
    {
        this.no=no;
        this.name=name;
        this.age=age;
        this.se=se;
    }
    public String toString(){
        return "no: " +no+ ", name:" +name+ ", age: " +age+ ", seminary: "+se;
    }
}
//---------------------------------------------------------------------------------------------------
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double sum1 = 0;
        double sum2 = 0;
        
            String C1 = sc.next();
            double C2 = sc.nextDouble();
            sum1 += C2;
            int C3 = sc.nextInt();
            sum1 += C3;
            String G = sc.next();
            String X1 = sc.next();
            int X2 = sc.nextInt();
            sum1 += X2;
            String S1 = sc.next();
            int S2 = sc.nextInt();
            sum1 += S2;
            String SS1 = sc.next();
            int SS2 = sc.nextInt();
            sum1 += SS2;

            // 读取第二部分输入
            String c1 = sc.next();
            double c2 = sc.nextDouble();
            sum2 += c2;
            int c3 = sc.nextInt();
            sum2 += c3;
            String g = sc.next();
            String x1 = sc.next();
            int x2 = sc.nextInt();
            sum2 += x2;
            String s1 = sc.next();
            int s2 = sc.nextInt();
            sum2 += s2;
            String ss1 = sc.next();
            int ss2 = sc.nextInt();
            sum2 += ss2;

            // 检查总和是否相等
            if (sum1 == sum2) {
                System.out.println("true");
            } else {
                System.out.println("false");
            }

            // 输出计算机1的信息
            System.out.println("Computer1:");
            System.out.println("CPU:");
            System.out.printf("Model: %s\n", C1);
            System.out.printf("Frequency: %.1f\n",C2);
            System.out.printf("Number of Cores: %d\n",C3);
            System.out.printf("Mainboard:\n");
            System.out.printf("Model: %s\n", G);
            System.out.printf("Memory:\n");
            System.out.printf("Model: %s\n", X1);
            System.out.printf("Size: %d\n",X2);
            System.out.printf("Screen:\n");
            System.out.printf("Model: %s\n", S1);
            System.out.printf("Size: %s\n", S2);
            System.out.printf("Harddisk:\n");
            System.out.printf("Model: %s\n", SS1);
            System.out.printf("Size: %d\n", SS2);

            System.out.println("Computer2:");
            System.out.println("CPU:");
            System.out.printf("Model: %s\n", c1);
            System.out.printf("Frequency: %.1f\n",c2);
            System.out.printf("Number of Cores: %d\n",c3);
            System.out.printf("Mainboard:\n");
            System.out.printf("Model: %s\n", g);
            System.out.printf("Memory:\n");
            System.out.printf("Model: %s\n", x1);
            System.out.printf("Size: %d\n",x2);
            System.out.printf("Screen:\n");
            System.out.printf("Model: %s\n", s1);
            System.out.printf("Size: %s\n", s2);
            System.out.printf("Harddisk:\n");
            System.out.printf("Model: %s\n", ss1);
            System.out.printf("Size: %d\n", ss2);

        }
    }

//-----------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //调用无参构造方法,并通过setter方法进行设值
        String sid1 = sc.next();
        String name1 = sc.next();
        int age1 = sc.nextInt();
        String major1 = sc.next();
        Student student1 = new Student();
        student1.setSid(sid1);
        student1.setName(name1);
        student1.setAge(age1);
        student1.setMajor(major1);
        //调用有参构造方法
        String sid2 = sc.next();
        String name2 = sc.next();
        int age2 = sc.nextInt();
        String major2 = sc.next();
        Student student2 = new Student(sid2, name2, age2, major2);
        //对学生student1和学生student2进行输出
        student1.print();
        student2.print();
    }
}
class Student{
    private  String sid ;
    private  String name ;
    private  int age ;
    private  String major ;
    public Student(String sid,String name,int age,String major)
    {
        this.sid=sid;
        this.name=name;
        this.age=age;
        this.major=major;
    }
    public Student()
    {
        this.sid=sid;
        this.name=name;
        this.age=age;
        this.major=major;
    }
    public void setSid(String sid){
        this.sid=sid;
    }
    public void setName(String name){
                this.name=name;
    }
    public void setAge(int age)
    {
        this.age=age;
    }
    public void setMajor(String major)
    {
        this.major=major;
    }
    public void print() {
        System.out.println("学号:" + sid + ",姓名:" + name + ",年龄:" + age + ",专业:" + major);
    }
}

标签:JAVA,String,int,age,System,public,name
From: https://www.cnblogs.com/Minyou03/p/18577468

相关文章

  • Java基础——泛型(3)#HashMap泛型
    一、HashMap        HashMap最早出现在JDK1.2中,底层基于散列算法实现,它是一个key-value结构的容器。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。二、HashMap特点      ......
  • JAVA基础教程---(一)JAVA语言概述
    JAVA基础教程JAVA语言概述前言JAVA之父JamesGosling团队在开发”Green”项目时,发现C缺少垃圾回收系统,还有可移植的安全性、分布程序设计和多线程功能。最后,他们想要一种易于移植到各种设备上的平台。Java确实是从C语言和C++语言继承了许多成份,甚至可以将Java看成是类......
  • Java集成Sa-Token进行认证与授权
    引言    软件开发过程中都必须要有的一个功能,那就是认证与授权,经过大佬们的不断更新迭代,使得如今实现认证与授权功能变得相对简单,也许你并不能真正的接触到认证与授权这一功能,除非你接触的项目是从0到1的,即便是这样的项目,认证与授权这一块依旧是系统架构师负责的,正因......
  • 深入理解Java中的类与对象
            在Java编程领域,类与对象是基石般的重要概念。为了清晰阐释其内涵、应用及相较于传统数据处理方式的优势,我们不妨从一个养猫的实际问题切入,逐步深入探究类与对象的世界。一、养猫问题引发思考        张老太养了两只猫咪,一只叫小白,3岁,毛色洁白;另一只......
  • Java面试之多线程&并发篇(9)
    前言本来想着给自己放松一下,刷刷博客,突然被几道面试题难倒!引用类型有哪些?有什么区别?说说你对JMM内存模型的理解?为什么需要JMM?多线程有什么用?似乎有点模糊了,那就大概看一下面试题吧。好记性不如烂键盘***12万字的java面试题整理******java核心面试知识整理******Java高频......
  • Java 使用魔数判断文件类型
    引言在日常的开发工作中,一般是使用文案后缀去判断文件类型,这种不是很严谨,那么这样可通过修改文件名的方式去修改文件类型,如果将一个危险文件修成成png或者txt上传到文件服务器,后果难以估量。那么可以使用文件头魔数的方式去判断文件类型,这是一种比较安全的方式。魔数介绍(Magic......
  • 拥抱 OpenTelemetry:阿里云 Java Agent 演进实践
    作者:陈承背景在2018年的2月,ARMSJavaAgent的第一个版本正式发布,为用户提供无侵入的的可观测数据采集服务。6年后的今天,随着软件技术的迅猛发展、业务场景的逐渐丰富、用户规模的快速增长,我们逐渐发现过去的功能以及架构的设计逐渐难以合理、优雅的满足今天的需求,重构越来......
  • Java小白学习之---抽象类
    一、引出方法不知道怎么写,写了也没意义,不如写成抽象方法╮(╯3╰)╭所谓抽象方法就是没有实现的方法所谓没有实现就是指,没有方法体当一个类中存在抽象方法时,需要将该类声明为abstract类二、抽象类介绍用abstract关键字来修饰一个类时,这个类就叫抽象类【访问修饰符ab......
  • 这些 JavaScript 编码习惯,让你最大程度提高你的项目可维护性!
    前言:因为JavaScript语言是一门极其松散、极其自由的语言,这意味着我们可以随心所欲的操作它,这是他的优点,但同时也是它的缺点。在编码过程中,我们需要一种良好的规范或者习惯来保持应用程序的一致性和可维护性。而今天我们要说的就是,怎么在日常编码中通过一些的良好的编码习惯,从你......
  • java jvm部分命令 ~~还在完善中
    命令整理jps-q只输出进程号-mmain函数的参数-l主类全名-v输出jvm参数jstatjstat-gcpid100010classgcgccapacitygcutilgccausegcnewgcnewcapacitygcoldgcoldcapacitycompilerprintcompilationgcmetacapacityjinfo-syspropspid-fl......