ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ZUCC_Object Oriented Programming_Lab05 Objects and Classes

2021-11-16 11:31:55  阅读:317  来源: 互联网

标签:Object Programming System public date Classes println class out


感谢LDingHui同学提供的代码

Lab Report 05

Note:

  • All your lab reports should be uploaded to BB before the deadline.

Caution

  • Must be original works, to prohibit any copying or plagiarism

一、 Experimental Purposes and Requirements

  1. to describe objects and classes, and use classes to model objects;
  2. to learn how to access objects;
  3. to distinguish between object reference variables and primitive data type variables.

二、Experimental Contents

1、(The Rectangle class) Following the example of the Circle class in Section 9.2(P323 Figure 9.3), design a class named Rectangle to represent a rectangle. The class contains:

  • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
  • A no-arg constructor that creates a default rectangle.
  • A constructor that creates a rectangle with the specified width and height.
  • A method named getArea() that returns the area of this rectangle.
  • A method named getPerimeter() that returns the perimeter.

Draw the UML(You are allowed not to draw UML, but if you want, you can!) diagram for the class and then implement the class. Write a test program that creates two Rectangle objects—one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BJ3AzVBg-1637033056757)(https://i.loli.net/2021/11/16/TRuvr1khU8DzcBg.jpg)]

Test Example:

img

2、(The Stock class) Following the example of the Circle class in Section 9.2(P323 Figure 9.3), design a class named Stock that contains:

  • A string data field named symbol for the stock’s symbol.
  • A string data field named name for the stock’s name.
  • A double data field named previousClosingPrice that stores the stock price for the previous day.
  • A double data field named currentPrice that stores the stock price for the current time.
  • A constructor that creates a stock with the specified symbol and name.
  • A method named getChangePercent() that returns the percentage changed from previousClosingPrice to currentPrice.

Draw the UML diagram for the class and then implement the class. Write a test program that creates a Stock object with the stock symbol ORCL, the name Oracle Corporation, and the previous closing price of 34.5. Set a new current price to 34.35 and display the price-change percentage.

img

Test Example:

img

3、(Use the Date class) Write a program that creates a Date object, sets its elapsed time to 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, and 100000000000, and displays the date and time using the toString() method, respectively.

Hint: you can use method setTime of Date class to set the elapsed time, if you do not know how to use the API, please read the online or offline reference document.( https://docs.oracle.com/javase/9/docs/api/overview-summary.html)

img

4、(Use the Random class) Write a program that creates a Random object with seed 1000 and displays the first 50 random integers between 0 and 100 using the nextInt(100) method.

Test Example:

img

5、(Use the GregorianCalendar class) Java API has the GregorianCalendar class in the java.util package, which you can use to obtain the year, month, and day of a date. The no-arg constructor constructs an instance for the current date, and the methods get(GregorianCalendar.YEAR), get(GregorianCalendar.MONTH), and get(GregorianCalendar.DAY_OF_MONTH) return the year, month, and day. Write a program to perform two tasks:

  • Display the current year, month, and day.
  • The GregorianCalendar class has the setTimeInMillis(long), which can be used to set a specified elapsed time since January 1, 1970. Set the value to 1234567898765L and display the year, month, and day.

Note: The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

Test Example:

img

三、Review questions

6、What is wrong with each of the following programs?

public class ShowErrors {
    public static void main(String[] args) {
        ShowErrors t = new ShowErrors(5);
    }
}
public class ShowErrors {
    public static void main(String[] args) {
        ShowErrors t = new ShowErrors();
        t.x();
    }
}
public class ShowErrors {
    public void method1() {
        Circle c;
        System.out.println("What is radius "
                + c.getRadius());
        c = new Circle();
    }
}
public class ShowErrors {
    public static void main(String[] args) {
        C c = new C(5.0);
        System.out.println(c.value);
    }
}

class C {
    int value = 2;
}

7、What is wrong in the following code?

class Test {
    public static void main(String[] args) {
        A a = new A();
        a.print();
    }
}

class A {
    String s;

    A(String newS) {
        s = newS;
    }

    public void print() {
        System.out.print(s);
    }
}

8、What is the output of the following code?

public class A {
    boolean x;
    public static void main(String[] args) {
        A a = new A();
        System.out.println(a.x);
    }
}

四、Please show your questions analysis, code and results.

1、

/**
 * @ClassName Rectangle
 * @Author OwemShu
 * @Description
 * @Date 2021/11/16    10:48
 **/

public class Rectangle {
    double weight = 1;
    double height = 1;

    Rectangle() {
    }

    public Rectangle(double weight, double height) {
        this.weight = weight;
        this.height = height;
    }

    double getArea() {
        return height * weight;
    }

    double getPerimeter() {
        return 2 * (height + weight);
    }
}
/**
 * @ClassName RectangleText
 * @Author OwemShu
 * @Description
 * @Date 2021/11/16    10:56
 **/

public class RectangleText {
    public static void main(String[] args){
        Rectangle r1 = new Rectangle(4,40);
        System.out.println("The area of a rectangle with width " + r1.weight + " and height " + r1.height + " is " + r1.getArea() + "\nThe perimeter of a rectangle is "+ r1.getPerimeter());
        Rectangle r2 = new Rectangle(3.5,35.9);
        System.out.println("The area of a rectangle with width " + r1.weight + " and height " + r1.height + " is " + r2.getArea() + "\nThe perimeter of a rectangle is "+ r2.getPerimeter());
    }
}

image-20211116105608073

2、

/**
 * @ClassName StockClass
 * @Author OwemShu
 * @Description
 * @Date 2021/11/16    10:49
 **/

public class priceChange {
    double previous = 1;
    double current = 1;
    priceChange(double pre, double cur){
        previous = pre;
        current = cur;
    }
    double getChangePercent() {
        return (current - previous) / previous * 100;
    }
}
/**
 * @ClassName priceChangeTest
 * @Author OwemShu
 * @Description
 * @Date 2021/11/16    11:02
 **/

public class priceChangeTest {
    public static void main(String[] args){
        priceChange p =new priceChange(34.5 , 34.35);
        System.out.println("Previous Closing Price: " + p.previous + "\nCurrent Price: " + p.current + "\nPrice Change: " + p.getChangePercent() + "%");
    }
}

image-20211116110518654

3、

import java.util.Date;

/**
 * @ClassName DateClass
 * @Author OwemShu
 * @Description
 * @Date 2021/11/16    10:49
 **/

public class DateClass {
    public static void main(String[] args){
        Date date = new Date(92,1,10);
        date.setTime(10000);
        System.out.println(date.toString());
        date.setTime(100000);
        System.out.println(date.toString());
        date.setTime(1000000);
        System.out.println(date.toString());
        date.setTime(10000000);
        System.out.println(date.toString());
        date.setTime(100000000);
        System.out.println(date.toString());
        date.setTime(1000000000);
        System.out.println(date.toString());
        date.setTime(10000000000L);
        System.out.println(date.toString());
    }
}

image-20211116110608679

4、

import java.util.Random;

/**
 * @ClassName RandomClass
 * @Author OwemShu
 * @Description
 * @Date 2021/11/16    10:49
 **/

public class RandomClass {
    Random ran = new Random(1000);
    void rand() {
        for( int i = 1; i <= 50 ;i++) {
            System.out.print(ran.nextInt(100) + " ");
            if(i % 10 == 0)
                System.out.print("\n");
        }
    }
}
/**
 * @ClassName RandomClassTest
 * @Author OwemShu
 * @Description
 * @Date 2021/11/16    11:06
 **/

public class RandomClassTest {
    public static void main(String[] args){
        RandomClass r = new RandomClass();
        r.rand();
    }
}

image-20211116110721418

5、

import java.util.GregorianCalendar;

/**
 * @ClassName GregorianCalendarClass
 * @Author OwemShu
 * @Description
 * @Date 2021/11/16    10:50
 **/

public class GregorianCalendarClass {
    public static void main(String[] args){
        GregorianCalendar gc = new GregorianCalendar();
        System.out.println("Year is " + gc.get(GregorianCalendar.YEAR) );
        System.out.println("Month is " + gc.get(GregorianCalendar.MONTH));
        System.out.println("Date is " + gc.get(GregorianCalendar.DAY_OF_MONTH));

        gc.setTimeInMillis(1234567898765L);
        System.out.println("Year is " + gc.get(GregorianCalendar.YEAR) );
        System.out.println("Month is " + gc.get(GregorianCalendar.MONTH));
        System.out.println("Date is " + gc.get(GregorianCalendar.DAY_OF_MONTH));
    }
}

image-20211116111037802

6、

  • ‘ShowErrors()’ 无法应用于 ‘(int)’
  • 无法解析 ‘ShowErrors’ 中的方法 ‘x’
  • 无法解析符号 ‘Circle’;变量 ‘c’ 可能尚未初始化;无法解析符号 ‘Circle’
  • ‘C()’ 无法应用于 ‘(double)’

7、

‘com.shuhx.Num7.A’ 中的 ‘A(java.lang.String)’ 无法应用于 ‘()’

8、

image-20211116112311645

标签:Object,Programming,System,public,date,Classes,println,class,out
来源: https://blog.csdn.net/OwemShu/article/details/121352175

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有