1  本题水题,主要理解题目的意思即可,访问方法和修改方法可以通过快捷方式alt+insert选中你需要的成员变量即可

 public class Person {
public String name;
public int age;
public static void main(String[] args) {
// new一个对象,对象名是person
Person person =new Person();
// 给name变量赋值
person.setName("zzy");
// 给age变量赋值
person.setAge(21);
// 对象的引用
person.speak();
} 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 void speak() {
System.out.println(getName());
System.out.println(getAge());
}
}

2  本题水题,跟着题目的意思写出需求即可,注意的是在无参的构造函数调用有参的构造函数的方法,本题的centerX与centerY没有用到

 public class Circle {
public double centerX,centerY,radius;
public static void main(String[] args) {
System.out.println("调用带参数的构造函数圆的各种数据:");
Circle circle=new Circle(1.0);
System.out.println("圆的半径:"+circle.getRadius());
System.out.println("圆的面积:"+circle.getRrea());
System.out.println("圆的周长:"+circle.getPerimeter());
System.out.println("调用不带参数的构造函数圆的各种数据:");
Circle circle1=new Circle();
System.out.println("圆的半径:"+circle1.getRadius());
System.out.println("圆的面积:"+circle1.getRrea());
System.out.println("圆的周长:"+circle1.getPerimeter()); }
//访问
public double getRadius() {
return radius;
} public void setRadius(double radius) {
this.radius = radius;
}
public double getRrea(){
return (Math.PI*radius*radius);
}
public double getPerimeter(){
return 2*Math.PI;
} public Circle(double radius) {
this.radius = radius;
}
public Circle() {
// 注意在无参的构造函数中调用有参的构造函数要用this
// 具体在书本71详解
this(1.0);
}
}

3  本题水题,注意在无参的构造函数调用有参的构造函数的方法

 public class Rectangle {
public double length,width; public static void main(String[] args) {
Rectangle rectangle =new Rectangle(1.0,1.0);
System.out.println("定义带参数的构造函数矩形:");
System.out.println("长方形的长为:"+rectangle.getLength());
System.out.println("长方形的宽为:"+rectangle.getWidth());
System.out.println("长方形的面积为:"+rectangle.getArea());
System.out.println("长方形的周长为:"+rectangle.getPerimeter());
System.out.println("定义不带参数的构造函数矩形");
Rectangle rectangle1 =new Rectangle(); System.out.println("长方形的长为:"+rectangle1.getLength());
System.out.println("长方形的宽为:"+rectangle1.getWidth());
System.out.println("长方形的面积为:"+rectangle1.getArea());
System.out.println("长方形的周长为:"+rectangle1.getPerimeter()); }
// 有参的构造方法
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// 无参的构造方法
public Rectangle(){
this(1.0,1.0);
} public double getLength() {
return length;
} public void setLength(double length) {
this.length = length;
} public double getWidth() {
return width;
} public void setWidth(double width) {
this.width = width;
}
// 矩形的周长
public double getPerimeter(){
return 2*(length+width);
}
// 矩形的面积
public double getArea(){
return length*width;
}
}

4  本题水题,主要是加深对构造方法的使用

 public class Triangle {
public double a,b,c,s; public static void main(String[] args) {
Triangle triangle = new Triangle(3,4,5);
System.out.println("调用带三个参数的构造函数:");
System.out.println("三角形的面积为:"+triangle.area());
System.out.println("调用默认构造函数:");
Triangle triangle1 =new Triangle();
System.out.println("三角形的面积为:"+triangle1.area()); }
// 有参的构造方法
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
s=(a+b+c)/2;
}
// 无参的构造方法
public Triangle() {
this.a = 0.0;
this.b = 0.0;
this.c = 0.0;
s=(a+b+c)/2;
}
// 三角形的面积
public double area(){
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
}

5  本题主要是加深对访问方法与修改方法的使用

 public class Stock {
public String symbol,name;
public double perviousPrice,currentPrice; public static void main(String[] args) {
Stock stock =new Stock("600000","浦发银行");
stock.setPerviousPrice(25.5);
stock.setCurrentPrice(28.6);
System.out.print("代号为:"+stock.symbol+"的"+stock.name);
System.out.print("当前的市值变化百分比为:"+String.format("%.1f",stock.getChangPercent())); }
// 返回从当前一日价格到当前价格变换的百分比
public double getChangPercent(){
return getCurrentPrice()-getPerviousPrice();
}
// 有参的构造方法
public Stock(String symbol, String name) {
this.symbol = symbol;
this.name = name;
}
// 返回储存股票的前一日收盘价
public double getPerviousPrice() {
return perviousPrice;
}
// 设置储存股票的前一日收盘价
public void setPerviousPrice(double perviousPrice) {
this.perviousPrice = perviousPrice;
}
// 返回存储股票的当前价格
public double getCurrentPrice() {
return currentPrice;
}
// 设置存储股票的当前价格
public void setCurrentPrice(double currentPrice) {
this.currentPrice = currentPrice;
}
}

6  Fibonacci数就是第一项第二项都是1从第三项开始是前两项之和,可以通过调用方法来实现,里面用if else语句

 public class Fibonacci {
// 调用方法
public static long fib(int n){
// 如果n==1或者2就返回1
if(n==1||n==2)
return 1;
else
return fib(n-2)+fib(n-1);
}
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
System.out.print(fib(i)+" ");
// 每10项进行一次换行
if (i % 10 ==0)
System.out.println();
} }
}

7  本题主要考的是方程的判别式问题 与方程的根求解问题,记住这些基本公式问题就不大

 public class QuadraticEquation {
private double a,b,c;
public static void main(String[] args) {
QuadraticEquation quadraticEquation =new QuadraticEquation(1,3,1);
if (quadraticEquation.getDiscriminant()<0)
System.out.println("方程无根");
if (quadraticEquation.getDiscriminant()==0)
System.out.println(quadraticEquation.getRoot1());
if (quadraticEquation.getDiscriminant()>0)
System.out.println("x1="+quadraticEquation.getRoot1()+" "+"x2="+quadraticEquation.getRoot2()); }
// 带有参数的构造方法
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
} public double getA() {
return a;
} public double getB() {
return b;
} public double getC() {
return c;
}
// 判别式
public double getDiscriminant(){
return b*b-4*a*c;
}
// 方程一个根
public double getRoot1(){
return (-b+Math.sqrt(Math.pow(b,2)-4*a*c))/2*a;
}
// 方程第二个根
public double getRoot2(){
return (-b-+Math.sqrt(Math.pow(b,2)-4*a*c))/2*a;
}
}

8  本题就是对成员变量的基本运用注意题目意思即可

 public class TV {
private int channel,volumeLevel;
private boolean on; public static void main(String[] args) {
}
// 无参构造函数
public TV(){
}
// 电视开
public void turnOn(){
on = true;
}
// 电视关
public void turnOff(){
on = false;
}
// 设置频道
public void setChannel(int channel) {
this.channel = channel;
}
// 设置音量
public void setVolumeLevel(int volumeLevel) {
this.volumeLevel = volumeLevel;
}
// 频道+开关
public void channelUp(){
channel++;
}
// 频道-开关
public void channelDown(){
channel--;
}
// 声音+开关
public void volumeUp(){
volumeLevel++;
}
// 声音-开关
public void volumeDown(){
volumeLevel--;
}
}

9  本题主要是对基本概念的理解,比如偶数、奇数的判断

 public class MyInteger {
private int value; public static void main(String[] args) { MyInteger myInteger =new MyInteger(10);
System.out.println("value:");
System.out.println("偶数:"+myInteger.isEven());
System.out.println("奇数:"+myInteger.isOdd());
System.out.println("素数:"+myInteger.isPrime());
System.out.println("返回参数15:");
System.out.println("偶数:"+myInteger.isEven(15));
System.out.println("奇数:"+myInteger.isOdd(15));
System.out.println("素数:"+myInteger.isPrime(15));
// char []a={'a','b','c'};
// myInteger.parseInt(a);
char A[]={'1','2','3'};
//myInteger.parseInt(A);
System.out.println(myInteger.parseInt(A));
String s = "123";
System.out.println(myInteger.parseInt(s));
} public MyInteger(int value) {
this.value = value;
} public int getValue() {
return value;
}
// 判断value是否为偶数
public boolean isEven(){
return value%2==0;
}
// 判断value是否为奇数
public boolean isOdd(){
return value%2!=0;
}
// 判断返回参数整数是否为素数
public boolean isPrime(){
int i;
for(i=2;i<value;i++) {
if (value % i == 0)
break;
}
if (value==i)
return true;
return false;
}
// 判断返回参数整数是否为偶数
public boolean isEven(int number){
return number%2==0; }
// 判断返回参数整数是否为奇数
public boolean isOdd(int number){
return number%2!=0; }
// 判断返回参数整数是否为素数
public boolean isPrime(int number){
int i;
for(i=2;i<number;i++) {
if (number % i == 0)
break;
}
if (number==i)
return true;
return false; }
// 判断返回参数整数对象是否为偶数
public boolean isEven(MyInteger myInteger){
return myInteger.value % 2 ==0; }
// 判断返回参数整数对象是否为奇数
public boolean isOdd(MyInteger myInteger){
return myInteger.value % 2 != 0;
}
// 判断返回参数整数对象是否为素数
public boolean isPrime(MyInteger myInteger){ int i;
for(i=2;i<myInteger.value;i++) {
if (myInteger.value % i == 0)
break;
}
if (myInteger.value==i)
return true;
return false;
}
// 比较当前对象整数与参数整数
public boolean equals(int b){
return b==value; }
// 比较当前对象整数与参数整数对象
public boolean equals(MyInteger myInteger){
return myInteger.value==value; }
// 将参数字符数组转换为整数
public int parseInt(char []a){
String s=new String(a);
return parseInt(s);
}
// 将参数字符串转换成整数
public int parseInt(String s){
return Integer.valueOf(s);
} }

10  本题主要考的是回文数与素数,我们首先判断这个数是否为素数,在这个基础上再判断是否为回文数,素数就是只能被1和本身整除的数,回文数就是一个数第一位与最后一位相等,第二位与倒数第二位相等,比如11、101

 public class HuiSu {
public int i,j,k,count=0; public static void main(String[] args) {
HuiSu huiSu =new HuiSu();
huiSu.print();
}
public void print(){
for ( j = 2; j < 1000 ; j++) {
for ( k = 2; k <1000 ; k++) {
if (j%k==0)
break;
}
// 判断是是否是素数
if (j == k)
{
// 判断是否为回文数
if (j / 10 == 0)
{
count++;
System.out.print(j+" ");
if (count % 10==0)
System.out.println();
}
if (j / 10==j%10)
{
count++;
System.out.print(j+" ");
if (count % 10==0)
System.out.println();
}
if(j / 100==j % 10)
{
count++;
System.out.print(j+" ");
if (count % 10 == 0 && count != 20)
System.out.println();
}
}
}
}
}

10  本题虽然是最后一题,但是仍然是水题,就是对成员变量的运用

 import java.time.LocalDate;

 public class Account {
private int id;
private double balance,annulRate;
private LocalDate dateCreated; public static void main(String[] args) {
Account account = new Account(123,0.0);
System.out.println("尊敬的:"+account.id+"用户"+"恭喜你开通账户");
System.out.println("你的账户余额为:"+account.balance);
//创建账户的时间
account.getDateCreated();
//存款
account.deposit(500);
//取款
account.withdraw(10);
System.out.println("你要办理的业务:");
account.setAnnulRate(3.6);
account.getMonthlyInterestRate(); }
// 有参构造函数
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
// 无参构造函数
public Account() {
} public int getId() {
return id;
}
// 返回账户余额
public double getBalance() {
return balance;
}
// 返回存款的年利率
public double getAnnulRate() {
return annulRate;
}
//获取当前日期
public LocalDate getDateCreated() {
// LocalDate dateCreated= LocalDate.now();
LocalDate today = LocalDate.now();
today.getYear();
System.out.println("您账户创建的日期为:"+LocalDate.now());
// System.out.println(today.getYear()+"/"+today.getMonthValue()+"/"+today.getDayOfMonth()+"/");
return dateCreated;
}
// 返回月利率的方法
public double getMonthlyInterestRate(){
System.out.println("您存款的月利率为:"+annulRate/12);
return annulRate/12; }
//取款方法
public void withdraw(double amount){
balance -= amount;
System.out.println("取款金额为:"+amount);
System.out.println("当前余额为:"+balance); }
// 存款方法
public void deposit(double amount){
balance += amount;
System.out.println("存款金额为:"+amount);
System.out.println("当前余额为:"+balance);
}
// 设置账户ID
public void setId(int id) {
this.id = id;
}
// 设置账户的余额
public void setBalance(double balance) {
this.balance = balance;
}
// 修改存款的年利率
public void setAnnulRate(double annulRate) {
System.out.println("您的存款年利率为:"+annulRate);
this.annulRate = annulRate;
}
}

最新文章

  1. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
  2. UI学习之常用方法
  3. Android 自定义对话框(Dialog)位置,大小
  4. C# 之【获取网页响应码200】
  5. MySQL查询语句执行过程及性能优化(JOIN/ORDER BY)-图
  6. 利用powershell进行远程服务器管理(命令行模式)
  7. 将Eclipse代码导入到Android Studio的两种方式
  8. POJ 3261 可重叠的 k 次最长重复子串【后缀数组】
  9. 显示进度条tqdm
  10. dicom错误解决
  11. 【RL-TCPnet网络教程】第32章 RL-TCPnet之Telnet服务器
  12. jmeter中判断数据库是否存在相应的记录
  13. ads出现村田电容电感无法仿真的问题解决(`BJT1&#39; is an instance of an undefined model `BJTM1&#39;)
  14. Linux命令之yum篇
  15. [转]C#程序性能优化
  16. 谨记:new Date()在IOS中的坑
  17. SEGMENTATION FAULT IN LINUX 原因与避免
  18. centos7设置SSH安全策略–指定IP登陆
  19. Linux 安装MySQL-python
  20. Sublime Text 的使用笔记

热门文章

  1. linux简单命令9--yum安装软件
  2. Python新利器之pipenv
  3. iOS实现页面既显示WebView,WebView下显示TableView,动态计算WebView内容高度
  4. 【POJ - 3641】Pseudoprime numbers (快速幂)
  5. 【Abode Air程序开发】打包并导出
  6. 【图像处理】FFmpeg解码H264及swscale缩放详解
  7. P1494 小Z的袜子 【普通莫队】
  8. Tcpdump移植
  9. [转帖] 龙芯 中标麒麟的 源 以及K8S
  10. [转帖]华为Mate20 X 5G版拆解:巴龙5000还配备了3GB独立内存!