JAVA语言程序设计课后习题----第四单元解析(仅供参考)
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;
}
}
JAVA语言程序设计课后习题----第四单元解析(仅供参考)的更多相关文章
- JAVA语言程序设计课后习题----第八单元解析(仅供参考)
1 本题主要考的是方法的克隆,与c++里面的拷贝有点相似,具体看书本p147 import java.util.Objects; public class Square implements Clon ...
- JAVA语言程序设计课后习题----第三单元解析(仅供参考)
1 本题水题,记住要知道输入格式即可 import java.util.Scanner; public class test { public static void main(String[] ar ...
- JAVA语言程序设计课后习题----第七单元解析(仅供参考)
1 本题水题,就是想让你理解继承的含义 public class Animaal { public double weight; public void eat(){ } } public class ...
- JAVA语言程序设计课后习题----第六单元解析(仅供参考)
1 本题就是基本函数的用法 import java.util.Scanner; public class Poone { public static void main(String[] args) ...
- JAVA语言程序设计课后习题----第五单元解析(仅供参考)
1 本题是水题,题目要求你求最大值.最小值,建议你用Arrays.sort函数进行排序,最大值.最小值就可以确定了 import java.util.Arrays; import java.util. ...
- JAVA语言程序设计课后习题----第二单元解析(仅供参考)
1 注意不同类型转换 import java.util.Scanner; public class Ch02 { public static void main(String[] args) { Sc ...
- JAVA语言程序设计课后习题----第一单元解析(仅供参考)
1 本题是水题,基本的输出语句 public class test { public static void main(String[] args) { // 相邻的两个 "" 要 ...
- Java语言程序设计(基础篇) 第四章 数学函数、字符和字符串
第四章 数学函数.字符和字符串 4.2 常用数学函数 方法分三类:三角函数方法(trigonometric method).指数函数方法(exponent method)和服务方法(service m ...
- Java语言程序设计-助教篇
1. 给第一次上课(软件工程)的老师与助教 现代软件工程讲义 0 课程概述 给学生:看里面的第0个作业要求 2. 助教心得 美国视界(1):第一流的本科课堂该是什么样?(看里面的助教部分) 助教工作看 ...
随机推荐
- 利用phpStudy 探针 提权网站服务器
声明: 本教程仅仅是演示管理员安全意识不强,存在弱口令情况.网站被非法入侵的演示,请勿用于恶意用途! 今天看到论坛有人发布了一个通过这phpStudy 探针 关键字搜索检索提权网址服务器,这个挺简单的 ...
- AOP获取方法注解实现动态切换数据源
AOP获取方法注解实现动态切换数据源(以下方式尚未经过测试,仅提供思路) ------ 自定义一个用于切换数据源的注解: package com.xxx.annotation; import org. ...
- solr 初接触
solr教程,值得刚接触搜索开发人员一看 http://blog.csdn.net/awj3584/article/details/16963525
- hutool-all 包把实体Bean转化成字符串,以及把字符串转化成Bean对象
GxyJobEntity gxyJobEntity1 = new GxyJobEntity(); gxyJobEntity1.setUserId("user001"); gxyJo ...
- c语言数组类型默认值(c99)
#include <stdio.h> #include <stdlib.h> int main() { ] = {}; //每个值默认0 ; i < len; i ++) ...
- CentOS8 使用 aliyun 阿里云 镜像站点的方法
CentOS8现已可使用国内的aliyun阿里云镜像站,方法如下: 用cd命令切换到yum.repos目录,备份原始的3个repo文件:cd /etc/yum.repos.d/sudo cp Cent ...
- Swift 3.0 Date的简单使用
// // ViewController.swift // Date的使用 // // Created by 思 彭 on 16/9/20. // Copyright © 2016年 思 彭. All ...
- C#中的索引器(Indexers)
前两天刚刚学习完了属性,这两天又搂完了索引器,发现两者非常的相似,但是相似之外还有一些不同之处.今天就来总结一下索引器--Indexers 索引器的作用及格式 索引器的作用就是能够使类或者结构体的实例 ...
- 【AMAD】django-allauth
简介 个人评分 简介 django-allauth1集成的Oauth API包括: Amazon (OAuth2) AngelList (OAuth2) Bitly (OAuth2) Dropbox ...
- [转帖]Grafana背后的Nginx和Apache Proxy
Grafana背后的Nginx和Apache Proxy https://ywnz.com/linuxyffq/5590.html 这个网站貌似非常非常好 在本文中,我将向你展示如何在Nginx和Ap ...