0% found this document useful (0 votes)
49 views3 pages

Tutorial For Week 10: Object Oriented Programming

The document provides instructions for students to generate code in Java for a Teacher and Student class based on a provided UML class diagram. It includes steps to draw the UML diagram, use a code generator to create the class files, and modify the generated code to include setter and getter methods. It also provides the code for the Student and Teacher classes and instructions for compiling and running the code to output student name and grades.

Uploaded by

Sucharita Saha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views3 pages

Tutorial For Week 10: Object Oriented Programming

The document provides instructions for students to generate code in Java for a Teacher and Student class based on a provided UML class diagram. It includes steps to draw the UML diagram, use a code generator to create the class files, and modify the generated code to include setter and getter methods. It also provides the code for the Student and Teacher classes and instructions for compiling and running the code to output student name and grades.

Uploaded by

Sucharita Saha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial for week 10: Object Oriented Programming

Task

Generate the code in Java using the UML class diagram for Teacher and Student Class.

Groups
This task is to be done in a group of three.

Process

1. Draw the UML diagram in Visual Paradigm.


2. Go to Tools section, and select Instant Generator, select Java, select classes for
Code generation (Student and Teacher), select output path and then select
Generate. You can also review the code by Preview button. The sample code will
be generated with class, attributes and methods. The method body will contain
some Exceptions. Remove this part of code and write the code given below for
different methods.
3. Students should use the sample code for Student and Teacher Class.

public class Student {


private String studentName;
private String studentGrade;

public Student(String name, String grade) {


studentName=name;
studentGrade=grade;
}

public Student() {

public void setStudentName(String name) {


studentName=name;

}
public void setGrade(String newgrade) {
studentGrade=newgrade;
}

public String getStudentName() {


return studentName;

public String getStudentGrade() {


return studentGrade;
}
}

public class Teacher {


private static String teacherName ="Miss Smith";

public Teacher() {

public String getTeacherName() {


return teacherName;
}

public static void main(String[] args) {


Student s1,s2;
s1 = new Student ();
s1.setStudentName("Mary");
s1.setGrade("A");
System.out.println("The student "+s1.getStudentName()+"has
a grade of"+s1.getStudentGrade());
s2= new Student ("John","B");
System.out.println("The student"+s2.getStudentName()+"has a
grade of "+s2.getStudentGrade());
}
}

Compile and Run (Unix)

To compile these classes:

$ javac Teacher.java Student.java

3. The Teacher class defines the main method, which is entry point for
this application. Run the Teacher class:

$ java Teacher

The student Mary has a grade of A


The student John has a grade of B

ClassPath Problems

If you get any message like this,

$ java Teacher
Can't find class Teacher

it probably means your CLASSPATH environment variable isn't properly set up. Make
sure it includes the current directory as well as the location where the classes.zip file was
installed. On Unix it should look something like this:

CLASSPATH=.:/usr/local/java-1.4/lib

Under Windows it will probably look something like this

C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip

Under Unix you set CLASSPATH variables like this:


csh: % setenv CLASSPATH my_class_path
sh: % CLASSPATH=my_class_path
You'll probably want to add one of these lines to your .login or .cshrc file so it will be
automatically set every time.

Under Windows you set the CLASSPATH environment variable with a DOS command
like

C:\> SET
CLASSPATH=C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip

You might also like