第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

java基礎(chǔ)練習(xí)——類的設(shè)計(jì)和對(duì)象的創(chuàng)建、成員變量和成員方法的設(shè)計(jì)

標(biāo)簽:
Java

和之前一样,依旧以题为例为大家讲述java类的设计和对象的创建、成员变量和成员方法的设计,欢迎大家交流讨论、
1、Point类例子(1)
请将下面程序的【代码】替换为Java程序代码,使程序运行正确。

文件Main.java
import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Point pt = 【代码1】 Point(1, n);// 创建对象
        System.out.println("pt.x=" + 【代码2】.x);// 取
        pt.x = 5;// 修改
        System.out.println("pt.x=" + pt.x);
        pt.move(3, 3);// 移动
        System.out.println("pt.x=" + pt.x);
        System.out.println("pt.y=" + pt.y);
        【代码3】 pt2 = new Point();// 声明对象并new
        System.out.println("pt2.x=" + pt2.x);
        pt2 = new Point(9, 2);
        System.out.println("pt2.x=" + pt2.x);
    }
}
class Point {
    int x, y;// 成员变量,属性
    【代码4】 Point() {// 无参构造方法
        x = 0;
        y = 0;
    }
    public Point(int ix, int 【代码5】) {// 有参构造方法
        x = ix;
        y = iy;
    }
    【代码6】 move(int ix, int iy) {// 方法
        x += ix;// x=x+ix
        y += iy;
    }
}

解题

import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Point pt = new Point(1, n);// 创建对象
        System.out.println("pt.x=" + pt.x);// 取
        pt.x = 5;// 修改
        System.out.println("pt.x=" + pt.x);
        pt.move(3, 3);// 移动
        System.out.println("pt.x=" + pt.x);
        System.out.println("pt.y=" + pt.y);
        Point pt2 = new Point();// 声明对象并new
        System.out.println("pt2.x=" + pt2.x);
        pt2 = new Point(9, 2);
        System.out.println("pt2.x=" + pt2.x);
    }
}
class Point {
    int x, y;// 成员变量,属性
    public Point() {// 无参构造方法
        x = 0;
        y = 0;
    }
    public Point(int ix, int iy) {// 有参构造方法
        x = ix;
        y = iy;
    }
    public void move(int ix, int iy) {// 方法
        x += ix;// x=x+ix
        y += iy;
    }
}

类描述图
类名称 Point
成员变量 int x,y
成员方法 void move(int is,int iy)
构造方法 Point() Point(int ix,int iy)

2、类的基本操作简单例子

文件Main.java
public class Main {
    public static void main (String args[ ]) {
       【代码1】//命令行窗口输出"教学活动从教室开始"
       Teacher zhang = new Teacher();
       Student jiang = 【代码2】Student();//创建对象
       zhang.introduceSelf();
       jiang.【代码2】; //调用它的方法
    }
}
class Teacher {
  void introduceSelf() {
     【代码3】    //命令行窗口输出"我是李老师"
   }
}
 
class Student {
  void introduceSelf() {
     【代码4】/ /命令行窗口输出"我是学生,名字是:奖励"
   }
}

解题

public class Main {
    public static void main (String args[ ]) {
       System.out.println("教学活动从教室开始");//命令行窗口输出"教学活动从教室开始"
       Teacher zhang = new Teacher();
       Student jiang = new Student();//创建对象
       zhang.introduceSelf();
       jiang.introduceSelf(); //调用它的方法
    }
}
class Teacher {
  void introduceSelf() {
     System.out.println("我是李老师");    //命令行窗口输出"我是李老师"
   }
}
 
class Student {
  void introduceSelf() {
	  System.out.println("我是学生,名字是:奖励");//命令行窗口输出"我是学生,名字是:奖励"
   }
}
类描述图
类名称 Teacher
成员方法 introduceSelf()
类名称 Student
成员方法 introduceSelf()

3、关于Point类的操作(2)
下面程序构造一个类来描述屏幕上的一个点,该类的构成包括点的 x 和 y 两个坐标,以及一些对点 进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并 对其进行操作。

文件Main.java
 public class Main {
 
public static void main(String[] args) {
Point Point1 = new Point(3, 4);
System.out.println("Point1:" + "(" +
Point1.x + "," + Point1.y + ")");
 
Point Point2 = Point1.getPoint();
System.out.println("Point2:" + "(" +
Point2.x + "," + Point2.y + ")");
 
Point 【代码1】 = new Point(5, 6);
Point1.setPoint(Point3);
System.out.println("Point1:" + "(" +
Point1.x + "," + Point1.y + ")");
}
}
 
class Point {
int x, y;
 
public Point(int x, 【代码2】) {
this.x = x;
this.y = y;
}
 
【代码3】 Point getPoint() {
Point tempPoint = new Point(x, y);
return tempPoint;
}
 
public void setPoint(Point point) {
this.x = point.x;
【代码4】y = point.y;
}
}
 

解题

public class Main {
 
public static void main(String[] args) {
Point Point1 = new Point(3, 4);
System.out.println("Point1:" + "(" +
Point1.x + "," + Point1.y + ")");
 
Point Point2 = Point1.getPoint();
System.out.println("Point2:" + "(" +
Point2.x + "," + Point2.y + ")");
 
Point Point3 = new Point(5, 6);
Point1.setPoint(Point3);
System.out.println("Point1:" + "(" +
Point1.x + "," + Point1.y + ")");
}
}
 
class Point {
int x, y;
 
public Point(int x, int y) {
this.x = x;
this.y = y;
}
 
public Point getPoint() {
Point tempPoint = new Point(x, y);
return tempPoint;
}
 
public void setPoint(Point point) {
this.x = point.x;
this.y = point.y;
}
 
}
类名称 Point
成员变量 Int x,y
成员方法 getPoint() setPoint(Point point)
构造方法 Point(int x,inty)

4、关于Point类的操作(3)
下面程序构造一个类来描述屏幕上的一个点,该类的构成包括点的 x 和 y 两个坐标,以及一些对点 进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并 对其进行操作。

文件Main.java
 
public class Main {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
Point origin = new 【代码1】(10, 10);
origin.getPoint();
【代码2】.setPoint(20, 20);
origin.getPoint();
}
 
}
 
class Point {
private int x;
private int y;
 
public Point(int x, int y) {
this.x = x;
【代码3】y = y;
}
 
public void setPoint(int x1, int y1) {
x = x1;
y = y1;
}
 
public void getPoint() {
System.out.println("Point x: " + x + ",y: " + y);
}
 
}

解题

public class Main {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
Point origin = new Point(10, 10);
origin.getPoint();
origin.setPoint(20, 20);
origin.getPoint();
}
 
}
 
class Point {
private int x;
private int y;
 
public Point(int x, int y) {
this.x = x;
this.y = y;
}
 
public void setPoint(int x1, int y1) {
x = x1;
y = y1;
}
 
public void getPoint() {
System.out.println("Point x: " + x + ",y: " + y);
}
 
}
类名称 Point
成员变量 private int x; private int y;
成员方法 setPoint(int x1, int y1) getPoint()
构造变量 Point(int x, int y)

5、按面向对象要求编程在Employee加入身份证
下面程序在Employee类中加入身份证信息,但类Employee中部分代码缺失.请编写程序代码,使程序运行正确。

文件Main.java
 
import java.util.*;
public class Main {
 public static void main(String args[]) {
  Scanner reader = new Scanner(System.in);
  String name=reader.nextLine();
  String ID=reader.nextLine();
  int salary= reader.nextInt();
  int year = reader.nextInt();
  int month=reader.nextInt();
  int day=reader.nextInt();  
Employee e = new Employee(name,ID,salary,year,month,day);
e.raiseSalary(5);
System.out.println("姓名:"+e. getName()+ " 身份证:" +e.getID()+" 工资:"+ e.getSalary());
  System.out.println("The Main class is end.");
 }
}
 
class Employee {
  private String name;//私有域,姓名
  private String ID;//私有域,身份证
  private double salary;//私有域,工资
  private Date hireDay; //私有域,录用日期
  //构造方法
  public Employee(String n, double s, int year, int month, int day)
  {
     name = n;//参数,(局部)变量
     salary = s;//参数, (局部)变量
     GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
     // GregorianCalendar uses 0 for January(1月份是0)
     hireDay = calendar.getTime();
  }
 
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
//"涨工资"计算
public void raiseSalary(double byPercent)
{
  double raise = salary * byPercent / 100;
  salary += raise;
}
 
}

输入:
姓名
身份证
工资
以空格分隔的日期,形式为YYYY MM DD
输出:
姓名:xxx 身份证:yyyyy 工资:d 这里xxx和yyyyy是字符串,d是double数
The Main class is end.

样例输入:
Romeo
430502199807101121
50000
2014 7 11
样例输出:
姓名:Romeo 身份证:430502199807101121 工资:52500.0
The Main class is end.
解题

import java.util.*;
public class Main {
 public static void main(String args[]) {
  Scanner reader = new Scanner(System.in);
  String name=reader.nextLine();
  String ID=reader.nextLine();
  int salary= reader.nextInt();
  int year = reader.nextInt();
  int month=reader.nextInt();
  int day=reader.nextInt();  
Employee e = new Employee(name,ID,salary,year,month,day);
e.raiseSalary(5);
System.out.println("姓名:"+e. getName()+ " 身份证:" +e.getID()+" 工资:"+ e.getSalary());
  System.out.println("The Main class is end.");
 }
}
 
class Employee {
  private String name;//私有域,姓名
  private String ID;//私有域,身份证
  private double salary;//私有域,工资
  private Date hireDay; //私有域,录用日期
  //构造方法
  public Employee(String n,String id, double s, int year, int month, int day)
  {
     name = n;//参数,(局部)变量
     salary = s;//参数, (局部)变量
     GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
     // GregorianCalendar uses 0 for January(1月份是0)
     hireDay = calendar.getTime();
  }
 
public String getID() {
	// TODO Auto-generated method stub
	return ID;
}

public String getName() {
return name;
}

public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
//"涨工资"计算
public void raiseSalary(double byPercent)
{
  double raise = salary * byPercent / 100;
  salary += raise;
}
 
}
类名称
成员变量 private String name; private String ID;private double salary; private Date hireDay;
成员方法 getID() getName() getSalary() getHireDay() raiseSalary(double byPercent)
构造方法 public Employee(String n,String id, double s, int year, int month, int day)
點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
Linux系統(tǒng)工程師
手記
粉絲
7200
獲贊與收藏
414

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消