老韩Java第七章作业汇总(自做)
韩老师(韩顺平)java第七章所有习题自己写的而且可以运行都放在这里了。
问题1:编写类A01,定义方法max,实现求某个double数组的最大值,并返回Homework01.java
public class HomeWork01 {
public static void main(String args[]){
double[] a = {
1.1,2.2,3.3,4.4,5.5};
//主函数
A01 b = new A01();
b.max(a);
}
//方法函数,最值获取
}
class A01{
public void max(double a[]){
double mx = a[0];
for(int i = 1;i<a.length;i++){
if(a[i] > mx){
mx = a[i];
}
}
System.out.println(mx);
}
}
问题2 编写类A02 ,定义方法find,实现查找某字符数组中的元素查找,并返回索引,如果找不到返回-1
public class HomeWork01 {
public static void main(String args[]){
String[] a = {
"aa","bb","cc","dd","ee"};
String st = "aa";
A02 b = new A02();
System.out.println("找到的字符串下标位:" + b.find(a,st));
//主函数
}
}
class A02{
//-1其实是一个哨兵岗
public int find(String a[],String st){
int idx = -1;
for(int i=0;i<a.length;i++){
if(st.equals(a[i])){
idx =i;
break;
}
}
return idx;
}
}
问题3 编写类Book,定义方法updatePrice,实现更改本书的价格,具体,如果价格>150,则更改为150,如果>100,更改为100,否则不变
public class HomeWork01 {
public static void main(String args[]){
Book b = new Book(159.2);
b.updatePrice();
System.out.println(b.price);
}
//方法函数,最值获取
}
class Book{
double price;
public Book(double v) {
price = v;
}
public void updatePrice(){
if(this.price > 150.0)
this.price = 150.0;
else if(this.price > 100)
this.price = 100.0;
}
}
问题4 编写类A03,实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样
public class HomeWork01 {
public static void main(String args[]){
int[] a = {
1,2,3,4,5};
A03 tmp = new A03();//临时对象
int[] b = tmp.CopyArray(a);
for (int ele:b
) {
//打印输出
System.out.println(ele);
}
}
}
class A03{
public int[] CopyArray(int a[]){
int[] b = new int[a.length];
for(int i=0;i<a.length;i++)
b[i] = a[i];
return b;
}
}
问题5 定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法
public class HomeWork01 {
public static void main(String args[]){
Circle c = new Circle(3.0);
c.printArea();
c.printPerimeter();
}
}
class Circle{
double r;
public Circle(double r) {
this.r = r;
}
public void printPerimeter(){
System.out.println("圆的周长" + 2*3.1415*this.r);
}
public void printArea(){
System.out.println("圆的面积" + this.r*3.1415*this.r);
}
}
问题6 编程创建一个Cale计算类,其中定义2个变量表示两个操作数,定义四个方法实现求和差乘商,除数为0给提示,并创建两个对象。
public class HomeWork01 {
public static void main(String args[]){
Cale c = new Cale(1.1,0.0);
c.div();
Cale d = new Cale(1.1,2.2);
d.add();
d.mul();
d.reduce();
}
//
}
class Cale{
double sum1;
double sum2;
public Cale(double v, double v1) {
sum1 = v;
sum2 = v1;
}
public void add(){
System.out.println("两数之和 " + (this.sum1+this.sum2));
}
public void reduce(){
System.out.println("两数之差 " + (this.sum1-this.sum2));
}
public void mul(){
System.out.println("两数之积 " + (this.sum1*this.sum2));
}
public void div(){
if((this.sum2-0.0)<1e-4){
System.out.println("除数为0");
}else{
System.out.println("两数之商" + (this.sum1/this.sum2));
}
}
}
问题7:设计一个Dog类,有名字颜色年龄,定义输出方法show()显示信息,并创建对象进行测试
public class HomeWork01 {
public static void main(String args[]){
Dog dg = new Dog("旺财","红色",2);
dg.printInfo();
}
//
}
class Dog{
String name;
String color;
int age;
public Dog(String s, String s1, int i) {
this.name = s;
this.color = s1;
this.age = i;
}
void printInfo(){
System.out.println("狗的名字叫" + this.name + ",颜色为:" + this.color + ",狗的年龄为:" + this.age );
}
}
问题8:给定一个java程序代码如下所示,则编译运行后,输出结果是()
public class Test {
int count = 9;
public void count1(){
count = 10;
System.out.println("count1 = " + count);
}
public void count2(){
System.out.println("count2 = " + count++);
}
public static void main(String args[]){
new Test().count1();
Test t1 = new Test();
t1.count2();
t1.count2();
}
}
问题9 定义music类,里面有音乐名name,音乐时长times属性,并有播放play功能和返回本身属性信息的功能方法getInfo
public class Music {
public static void main(String[] args){
Mus mu = new Mus("青花瓷",242);
mu.Play();
mu.getInfo();
}
}
class Mus{
String name;
int time;
public Mus(String name, int i) {
this.name = name;
this.time = i;
}
void Play(){
System.out.println("音乐播放中....");
}
void getInfo(){
System.out.println("歌曲名称:" + this.name + ",歌曲时长" + this.time/60 + "分" + this.time%60 + "秒");
}
}
问题10 试写出以下代码运行结果()
public class Demo{
public static void main(String args[]){
Dem d1 = new Dem();
Dem d2 = d1;
d2.m();
System.out.println(d1.i);
System.out.println(d1.i);
}
}
class Dem {
int i = 100;
public void m(){
int j = i++;
System.out.println("i="+i);
System.out.println("j="+j);
}
}
问题11 在测试方法中,调用method方法,代码如下,编译正确,调用语句System.out.println(method(method(10.0,20.0),100));
public class HomeWork11 {
public double method(double a,double b){
return a+b;
}
static int method(double a, int b){
return (int) (a + (double)b);
}
public static void main(String args[]) {
System.out.println(method(method(10.0, (int) 20.0), 100));
}
}
问题12 创建一个Employee类,属性有(名字,性别,年龄,职位,薪水),提供三个构造方法,1.(名字,性别,年龄,职位,薪水),2,(名字,性别,年龄),(3)(职位,薪水)
public class HomeWork12 {
public static void main(String args[]){
Employee e1 = new Employee("高级工程师",15500);
Employee e2 = new Employee("张怀",'男',22);
Employee e3 = new Employee("李华",'男',23,"中级工程师",15500);
System.out.println(e3.age);
System.out.println(e3.name);
System.out.println(e3.gender);
System.out.println(e3.position);
System.out.println(e3.salary);
}
}
class Employee{
String name;
char gender;
int age;
String position;
int salary;
public Employee(String position, int salary) {
this.position = position;
this.salary = salary;
}
public Employee(String name, char gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public Employee(String name, char gender, int age, String position, int salary) {
this(name,gender,age);
this.position = position;
this.salary = salary;
System.out.println("构造函数.....");
}
}
问题13
import javax.swing.plaf.basic.BasicIconFactory;
public class HomeWork13 {
public static void main(String args[]){
PassObject ps = new PassObject();
Circle c = new Circle();
ps.printAreas(c,5);
}
}
class Circle{
double r;
double findArea(){
return 3.14159*r*r;
}
}
class PassObject{
public void printAreas(Circle c,int times){
System.out.println("Radius\t\tArea");
for(int i =1;i<=times;i++){
c.r = (double) i;
System.out.println(c.r + "\t\t" + c.findArea());
}
}
}
还没有评论,来说两句吧...