首页 > 其他分享 >SD6502 Programming Person

SD6502 Programming Person

时间:2024-10-22 21:02:15浏览次数:1  
标签:SD6502 get abstract Programming public Person Student class

SD6502 Programming II 1Lab Work

continued from last lab(week 5)

Task 1:Inheritance

  1. Let’s add a few more classes to the PolytechLibrary which you created in last lab session.(a) Add a class and name it Teacher.cs(b) Add attributes(fields) such as FirstName, LastName, Email Address and Subject as properties.(c) Add another class name it Student.cs (d) Add some fields(properties), FirstName, LastName . . . .Wait, didn’t you add same fields already for teacher class. Teacherand students have things in common such as firstname, lastname, email address and so on. Unlike Teacher, students do not havesubject to teach. We can make use of a OO concept called inheritance here. What we can do is definea class, say person, and put all the common fields (attributes) there. Then make Student and Teacher classes inherit all the common attribute from Person class. Of course, we will add the unique fields oftheir own in their class definition.
  1. Add a class person
  2. Add common attributes to Person class and remove those from Teacher and student classpublic class Person {

public string FirstName { get; set; }

public string LastName { get; set; }

public string Email { get; set; }

}

Also,

Teacher and Student should now look like this,

public class Teacher : Person {

public string Subject { get; set; }

}

public class Student : Person {

}

Note: The class names Teacher and Student is followed by a colon and then Person. The Personclass is called base or parent class. Similarly Teacher and student classes are called derived or childor subclass. Also, if you are working with VS 2022 replace internal keyword public. VS2022/.Netframework 6 automatically adds internal keyword when you add a new class file.

.SD6502 Programming II 2

One other thing, if you don’t want others who are using the 代 写SD6502 Programming II 1Lab   PolytechLibrary to be able to use thePerson class directly(by directly I mean creating an object and using it from their code) you can makeperson class abstract. In this way you are making others to use Teacher and Student class directlyfrom their code.

  1. Add a keyword abstract before the class keyword in the definition of person classpublic abstract class Person {

public string FirstName { get; set; }

public string LastName { get; set; }

public string Email { get; set; }

}

Let’s add some attributes(feilds) to the empty Student class. A attribute called GradLevel to showwhat level they are studying in. What type of data a GradLevel would hold ? In our polytech wehave level 5, level 7 and level 8 graduates. How would you model this fixed but discrete set ofdata ? Enumeration datatype are perfectly suited in this situation. So let’s define a enum and call itGradlevels. Also, add a property to get and set Gradlevels field.

public class Student: Person {

public enum GradLevels {Level_5, Level_7, Level_8, Level_9 }

public GradeLevels GradLevel {get; set; }

}

Task 2: Working with derived class objects

Let’s go back to our PolytechFormApp. Double click the button to get the source code.Try making it a new Person, i.e. create an object of Person class. Did you get any error why?Create Teacher and Student objects and try and access all the fields.

Task 3:Abstract methods

Abstract classes are classes that are designed such that you can’t instantiate them or use them directly. The

same is true for abstract methods. An abstract method is a method with no code in it. Let’s say, for example,we want to implement a method that will allow us to get a grade point average, but it’s going to be a differentcomputation depending on whether you’re talking about the teacher or the student. The student is going tohave the average of all the grades for the semester or the period or whatever time period we’re talking aboutand the teacher’s grade point average is going to bethe average grade point average for all of the studentsin this class. So it makes sense to have two different, completely different implementations of a function bythe same name. So create the abstract method and then override it in each of the subclasses with specific

mplementations for those classes.

.SD6502 Programming II 3

public abstract float ComputeGradeAverage ( ) ;

In Student class

public override float ComputeGradeAverage ( ) {

return 4.0f;

}

In Teacher class

public override float ComputeGradeAverage ( ) {

//TODO: fix the implementation later

return 0.0f;

}

Note: If a method(s) is declared abstract in base class all derived classes must have its definition (im

plementation). In this case method ComputeGradeAverage must be implemented by Student and Teacherclasses. Otherwise, the code won’t compile. Also, note thatyou can also declare any method abstract innon-abstract class.

Task 4: Testing the abstract method

Now that you have an abstract method and an overridden version of it in our two subclasses, why don’t wetake a look at what it looks like in our test program ?Add two buttons and write code to test the overridden version of ComputeGradeAverage from bothclasses.

Task 5:

Do all the examples exercise from Lecture slides. Codes are given here in the folder, try to understand:

  1. How base and derived classes work in C#
  2. How polymorphism is achieved in C#
  3. What does extension methods do in C# and why it is useful?

Task 5:

Start working on your assignment.

Submission

NO submission needed. You will continue this work in the next lab. Show to your lab instructor when youcomplete week 7 lab.Credits: C# essentials, Bruce Van Horn.

.

标签:SD6502,get,abstract,Programming,public,Person,Student,class
From: https://www.cnblogs.com/goodlunn/p/18493554

相关文章

  • SD6502 Programming prototyping techniques
    SD6502ProgrammingIIAssignmentIIDueDate:23rdOctober2024,11:59pmTopics(LOs):ApplyprototypingtechniquesApplyeffectiveproblem-solvingstrategiestofosterprogrammingskills.Weighting=25%oftheFinalMark.TotalMarks=100.Group......
  • 2024 BUPT Programming Contest F
    简要题意给定一个\(n\timesn\)矩阵,矩阵中的每一个元素的计算方式如下:随机从\([0,n-1]\)中选出两个整数(可以重复)\(a,b\),矩阵的元素为\(a\timesb\bmodn\)求矩阵中元素\(m\)出现次数的期望。\(0\lem<n\le10^{12}\)题解对于矩阵中的任意一个元素是独......
  • Multithreaded programming
    Lab02:MultithreadedprogrammingDuedatePleaserefertothelabassignmentrequirements.GoalThegoalofthisprojectis(1)toobtainagoodunderstandingofmulti-threading,(2)topracticecreatingthreadsandcoordinatetherunningofthethreads.......
  • 2024 ICPC Asia Taiwan Online Programming Contest题解记录
    比赛链接:https://codeforces.com/gym/105383/problemA.AnimalFarm找个最大pig,然后所有比他小的其他种类生物一直加就好了#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constllmod=1e9+7;llksm(llx,lly){ llans=1; while(y) { if(y&1)......
  • CS 551 Systems Programming
    CS551SystemsProgramming,Fall2024ProgrammingProject1Out:10/13/2024Sun.Due:10/26/2024Sat.23:59:59Inthisprojectyouraregoingtoimplementacustommemorymanagerthatmanagesheapmemoryallocationatprogramlevel.Herearethereasonswh......
  • Expression-bodied members (C# programming guide)
    Expressionbodydefinitionsletyouprovideamember'simplementationinaconcise,readableform.Youcanuseanexpressionbodydefinitionwheneverthelogicforanysupportedmember,suchasamethodorproperty,consistsofasingleexpression.A......
  • TECH.UB.25: Intro to Python Programming
    TECH.UB.25:IntrotoPythonProgramming:Assignment#4Scenario: CampusPizzaisreallytakingoffandyourco-founderslovetheprogramsyouhavebuilt. Theywantyoutobuildanobject-orientedprogramfortheirbeverages. Campuspizzahastwobeve......
  • C - Word Ladder (Toyota Programming Contest 2024#9 (AtCoder Beginner Contest 370)
    题目链接:C-WordLadder题目:样例:分析:不要被题目所吓到,一切长题目都是纸老虎。题目大意就是给你两个字符串s和t,一次只能更换一个字母,求s变到t更换的次数,并输出每次更换一个字母后的最小字典序字符串。题意好理解,可以直接暴力,大力出奇迹。但是有没有更好的方法呢?既然问了......
  • HIAST Collegiate Programming Contest 2024(非完全题解)
    C题HZY做的,等他补题解//#pragmaGCCoptimize("O3,unroll-loops")//#pragmaGCCtarget("avx2,bmi,bmi2,lzcnt,popcnt")////如果在不支持avx2的平台上将avx2换成avx或SSE之一#include<bits/stdc++.h>usingnamespacestd;#definexfirst#defineysecon......
  • 【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest I
    SteadilyGrowingSteam#动态规划#背包#枚举题目描述AliceenjoysplayingacardgamecalledSteadilyGrowingSteam(asknownasSGS).Inthisgame,eachplayerwillplaydifferentrolesandhavedifferentskills.Playersgetcardsfromthedeckandu......