Dart's four assignments

1.

Define a Student class. In addition to stuID, stuName, stuClass, stuGrade and stuDept member variables, the class is also designed with Student Newstudent (this. Studid), factory Student(String studid) construction method and String toString() member method. Please implement this class, instantiate and verify the designed and implemented class in main (), and make brief and appropriate comments on the verification output. The main() verification code is similar to the following:

main() {

  Person student1 = new Student('14194703748');

  print(student1.hashCode);

  Person student2 = new Student('14194703751');

  print(student2.hashCode);

  Person student3 = new Student('14194703748');

  print(student3.hashCode);

  

  assert(student1 == student3);

  print(student1 == student3);

  print(identical(student1, student3)); / / judge whether the variables point to the same block of memory

}

 

reference:

Dart JSON and serialization

https://www.jianshu.com/p/34c45dc28305

class Student {

  String stuID;

  String stuName;

  String stuGrade;

  String stuDept;



  factory Student(String stuID) {

    return Student.newStudent(stuID);

  }

  Student.newStudent(this.stuID) {

    stuName = null;

    stuGrade = null;

    stuDept = null;

  }

  String toString() {

    return "stuID:" +

        this.stuID +

        "\nstuName:" +

        this.stuName +

        "\n" +

        "\nstuGrade:" +

        this.stuGrade +

        "\nstuDept" +

        this.stuDept;

  }

}



main() {

  Student student1 = new Student('14194703748');

  print(student1.stuID.hashCode);

  //stay Object Class, each object has a default hash code whose value is the storage address of the object.

  Student student2 = new Student('14194703751');

  print(student2.stuID.hashCode);

  Student student3 = new Student('14194703748');

  print(student3.stuID.hashCode);

  assert(student1 == student3);

  print(student1.stuID == student3.stuID);

  print(identical(student1.stuID, student3.stuID)); //Judge whether the variables point to the same block of memory

}

// Output:

// 444574562

// 666782041

// 444574562

// true

// true

2.

There are students JSON file, as follows:

{

    "id": "10086",

    "name": "Jack",

    "phone": "13311112222"

}

In addition, a Student class is designed. The class contents are as follows:

class Student {

  String stuId;

  String stuName;

  String stuTel;

 

  Student({this.stuId, this.stuName, this.stuTel});

 

  factory Student.fromJson(Map<String, dynamic> json) {

    return Student(

        stuId: json["id"], stuName: json["name"], stuTel: json["phone"]);

  }

}

 

Now it is required to read person from disk asynchronously json file, get a json string, and then use json Decode (string string) parses the json string to get a Map object. Finally, use Student From Jason (Map < string, dynamic > json) establishes a connection with Student The instance object of Student type corresponding to the content in json and output it.

import 'dart:io';

import 'dart:convert';



class Student {

  String stuId;

  String stuName;

  String stuTel;

  Student({this.stuId, this.stuName, this.stuTel});

  factory Student.fromJson(Map<String, dynamic> json) {

    return Student(

        stuId: json["id"], stuName: json["name"], stuTel: json["phone"]);

  }

  String toString() {

    return "id:" +

        this.stuId +

        "\nname:" +

        this.stuName +

        "\nTel:" +

        this.stuTel;

  }

}



void main() async {

  File file = new File('student.json');

  try {

    String k = await file.readAsString();

    print("File read succeeded!");

  } catch (error) {

    print(error);

  }

  String k = await file.readAsString();

  Student student1 = Student.fromJson(jsonDecode(k));

  print(student1.toString());

}

//copyright@halfgong

3.

Define an abstract class Shape, which has abstract methods getPerimeter() and getArea().

Define a Rectangle class to inherit the Shape abstract class and override the abstract methods getPerimeter() and getArea() in the Shape abstract class.

Define a Cuboid box class to inherit the Rectangle class, which contains a double type member variable height representing height; Define the construction method Cuboid(double length, double width, double height); Then define the member method volume() to calculate the volume of the box.

Create an instance of Rectangle class and Cuboid class in main() to test. For example, find the volume of a Cuboid with length, width and height of 10, 5 and 2 respectively.

Finally, define the Square class and Cube class. Think about how Square class and Cube class can inherit and implement the existing Shape class, Rectangle class and Cuboid class. Further test the Square class and Cube class defined in main().

 

abstract class Shape {

  double getPerimeter();

  double getArea();

}



class Rectangle extends Shape {

  double length;

  double width;

  Rectangle(this.length, this.width);

  double getPerimeter() {

    return this.length * 2 + this.width * 2;

  }



  double getArea() {

    return this.length * this.width;

  }

}



class Cuboid extends Rectangle {

  double high;

  Cuboid(double length, double width, this.high) : super(length, width) {}

  double volume() {

    return this.high * this.length * this.width;

  }

}



class Square extends Shape {

  double edge;

  Square(this.edge);

  double getPerimeter() {

    return this.edge * 4;

  }



  double getArea() {

    return this.edge * this.edge;

  }

}



class Cube extends Square {

  Cube(double edge) : super(edge) {}

  double volume() {

    return this.edge * this.edge * this.edge;

  }

}



main() {

  Rectangle R = new Rectangle(1, 3);

  print(R.getArea());

  Cuboid C = new Cuboid(1, 3, 5);

  print(C.volume());

  Square S = new Square(2);

  print(S.getArea());

  Cube Cu = new Cube(2);

  print(Cu.volume());

}



// result:

// 3.0

// 15.0

// 4.0

// 8.0

//copyright@Gong ban

4.

There are the following Rectangle classes. Please define two calculation properties for this class: right and bottom

class Rectangle {

  num left;

  num top;

  num width;

  num height;

 

  Rectangle(this.left, this.top, this.width, this.height);

 

/ / define two calculation attributes: right and bottom

  //...

}

Then test the Rectangle class in main(). The test code is similar to the following:

main() {

  var rect = new Rectangle(3, 4, 20, 15);

  assert(rect.left == 3);

  print('right: ${rect.right}, bottom: ${rect.bottom}');

  rect.right = 12;

  //set right, then calculate left with width: left = right - width

  assert(rect.left == -8);

  print('left: ${rect.left}, top: ${rect.top}');

}

class Rectangle {

  num left;

  num top;

  num width;

  num height;

  Rectangle(this.left, this.top, this.width, this.height);

  // Define two calculation properties: right and bottom.

  //...

  num get right {

    num right;

    right = left;

    return right;

  }



  num get bottom {

    num bottom;

    bottom = top;

    return bottom;

  }



  num set right(num right) {

    num left;

    left = right - width;

    return left;

  }

}



main() {

  var rect = new Rectangle(3, 4, 20, 15);

  assert(rect.left == 3);

  print('right: ${rect.right}, bottom: ${rect.bottom}');

  rect.right = 12;

  //set right, then calculate left with width: left = right - width

  assert(rect.left == -8);

  print('left: ${rect.left}, top: ${rect.top}');

}

//result:

// right: null, bottom: null

// left: 3, top: 4

//copyright@Gong Zicheng

 

Posted by swr on Sat, 14 May 2022 02:39:16 +0300