0% found this document useful (0 votes)
42 views31 pages

Lab 3

Uploaded by

krupan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views31 pages

Lab 3

Uploaded by

krupan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

/*

Dipen Patel

CSC 236

Lab-3A

*/

public interface PolynomialADT

//ADT class hold all the method with no Body.

public boolean isEmpty();

public void setFirstNode(PolyNode a);

public PolyNode getFirstNode();

public void addPolyNodeFirst(int c,int e);

public void addPolyNodeLast(int c, int e);

public int getHighestDegree(PolyNode a);

public void addPolynomial(PolyNode a);

}
/*

Dipen Patel

CSC 236

Lab-3A

*/

public class PolyNode

// variables for info, info2 and link

public int info;

public int info2;

public PolyNode link;

//Default Constructor.

public PolyNode()

info=0;

info2=0;

link=null;

//OverLoaded Constructor.

public PolyNode(PolyNode p)

this.info=p.info;

this.info2=p.info2;

this.link=p.link;

//OverLoaded Constructor

public PolyNode(int c, int e)


{

info=c;

info2=e;

link=null;

//OverLoaded Constructor

public PolyNode(int c, int e, PolyNode next)

info=c;

info2=e;

link=next;

//set Coefficient method.

public void setCoefficient(int c){

info=c;

//get Coefficient method.

public int getCoefficient(){

return info;

//set exponent method.

public void setExponent(int e)

info2=e;

//get Exponent method.

public int getExponent(){


return info2;

//set next method.

public void setNext(PolyNode n){

link= n;

//get next method.

public PolyNode getNext(){

return link;

}
/*

Dipen Patel

CSC 236

Lab-3A

*/

import java.util.Random;

//Implements the PolynomialADT.

public class PolynomialDataStructure implements PolynomialADT{

//Variable of type polynode.s

PolyNode headPointer;

//Default constructor

public PolynomialDataStructure(){

this.headPointer = new PolyNode();

//OVERLOADED CONSTRUCTOR

public PolynomialDataStructure(PolynomialDataStructure p){

this.headPointer=p.headPointer;

//OVERLOADED CONSTRUCTOR

public PolynomialDataStructure(int c, int e){

headPointer = new PolyNode(c, e);

//IS EMPTY METHOD CHECKS IF THE NODE IS EMPTY OR NOT.

public boolean isEmpty(){

if(headPointer.getCoefficient()==0&&headPointer.getExponent()==0)

return true;
else

return false;

//SETS THE FIRST NODE.

public void setFirstNode(PolyNode p){

headPointer = p;

//GETS THE FIRST NODE.

public PolyNode getFirstNode(){

return headPointer;

//ADD POLYNODE TO THE BEGINNING

public void addPolyNodeFirst(int c, int e){

if(isEmpty())

headPointer = new PolyNode(c, e, null);

else

headPointer = new PolyNode(c, e, headPointer);

//ADDS NODE IN THE END.

public void addPolyNodeLast(int c, int e){ //C is coefficient and e is exponent

if(isEmpty())

headPointer = new PolyNode(c, e, null);

else{

PolyNode current = headPointer;

while(current.getNext()!=null)

current=current.getNext();
current.setNext(new PolyNode(c, e, null));

//RETURNS THE HIGHEST DEGREE FROM NODES.

public int getHighestDegree(PolyNode p){

int higestDegree=0;

PolyNode current = p;

higestDegree=current.getExponent();

while(current.getNext()!=null){

if(current.getExponent()>higestDegree)

higestDegree=current.getExponent();

current=current.getNext();

if(current.getExponent()>higestDegree)

higestDegree=current.getExponent();

return higestDegree;

//ADDS THE POLYNOMAILS.

public void addPolynomial(PolyNode a){

boolean added = false;

PolyNode current = headPointer;

PolyNode current2= a;
PolyNode temp2=current2;

PolyNode newNode = new PolyNode(1,1);

PolyNode temp=newNode;

Random rnd = new Random();

while(current!=null)

while(temp2!=null)

if(current.getExponent()==temp2.getExponent()){

temp.setNext(new PolyNode(current.getCoefficient()

+temp2.getCoefficient(),current.getExponent(),null));

current.setExponent(rnd.nextInt(1000)+1000);

temp2.setExponent(rnd.nextInt(1000)+1000);

temp=temp.getNext();

temp2=temp2.getNext();

if(current.getExponent()<100){

temp.setNext(new PolyNode(current.getCoefficient()

, current.getExponent()));

temp=temp.getNext();

current.setExponent(rnd.nextInt(1000)+1000);

temp2=current2;

current=current.getNext();

}
temp2=current2;

current = headPointer;

while(temp2!=null){

while(current!=null){

if(temp2.getExponent()==current.getExponent()){

temp.setNext(new PolyNode(temp2.getCoefficient()

+current.getCoefficient()

,temp2.getExponent(),null));

temp2.setExponent(rnd.nextInt(1000)+1000);

temp=temp.getNext();

current=current.getNext();

if(temp2.getExponent()<10){

temp.setNext(new PolyNode(temp2.getCoefficient()

, temp2.getExponent()));

temp=temp.getNext();

temp2.setExponent(rnd.nextInt(1000)+1000);

temp2=temp2.getNext();

current=headPointer;

headPointer=newNode.getNext();

//TO STRING METHOD.

public String toString(){


if(isEmpty())

return "Empty";

else{

String s = "";

PolyNode current = headPointer;

while(current!=null){

s=s+(current.getCoefficient()+"x^"+current.getExponent()+"+");

current = current.getNext();

return s;

}
/*

Dipen Patel

CSC 236

Lab-3A

*/

public class PolynomialDemo

public static void main(String[]args)

/*CREATES AN OBJECT OF TYPE POLYNOMAIL DATA STRUCTURE

* AND ADD FIRST AND LAST NODES.

*/

PolynomialDataStructure obj1= new PolynomialDataStructure(4,3);

PolynomialDataStructure obj2= new PolynomialDataStructure(3,5);

PolynomialDataStructure obj3= new PolynomialDataStructure(-5,0);

PolynomialDataStructure obj4= new PolynomialDataStructure(-4,0);

obj1.addPolyNodeLast(3, 2);

obj1.addPolyNodeLast(-5, 0);

obj2.addPolyNodeLast(4, 4);

obj2.addPolyNodeLast(1, 3);

obj2.addPolyNodeLast(-4, 2);

obj2.addPolyNodeLast(4, 1);

obj2.addPolyNodeLast(2, 0);

obj3.addPolyNodeLast(3, 2);

obj3.addPolyNodeLast(4, 3);

obj4.addPolyNodeLast(4, 3);

obj4.addPolyNodeLast(5, 4);

/*
* PRINTS OUT ALL THE CREATED OBJECTS.

*/

System.out.println("p1: " +obj1);

System.out.println("p2: " +obj2);

System.out.println("p3: " +obj3);

System.out.println("p4: " +obj4);

/*

* ADDS ALL THE POLYNOMAIL AND PRINTS IT OUT.

*/

obj1.addPolynomial(obj2.getFirstNode());

System.out.println("p1 added to p2: " +obj1.toString());

obj3.addPolynomial(obj4.getFirstNode());

System.out.println("p3 added to p4: " +obj3.toString());

}
/*

Dipen Patel

CSC 236

Lab-3B

*/

public interface StudentsInterface

//ADT class hold all the method with no Body.

public void setMaxNumberOfStudents(int maxNumOfStudents);

public void addStudent(int StudentsID);

public void addCourse(int StudentsId, Course c);

public void dropCourse(int StudentsID, Course c);

public String toString();

}
/*

Dipen Patel

CSC 236

Lab-3B

*/

import java.util.NoSuchElementException;

public class Students

//Pirvate Variables.

private int currentNumOfStudents;

private Student[] s;

private int maxNumOfStudents;

//Inner Class.

public class Student

// Private variables

private int studentID;

private Course course;

//Default constructor.

public Student()

studentID = 0;

//Overloaded constructor.
public Student(int studentsId)

studentID = studentsId;

//SETS THE IDS

public void setID(int ID)

studentID = ID;

//SETS THE COURSE

public void setCourses(Course c)

course = c;

//GETS THE COURSE.

public Course getCourses()

return course;

//GETS THE ID

public int getID()

return studentID;

//ADDS TEH COURSE

public void addCourse(Course couseadded)

if(this.getCourses() == null)
course = new Course(couseadded.getCourseName(),

couseadded.getSectionNumber(),

couseadded.getCredits(), null);

else

Course c = this.course;

while(c.getLink() != null)

c = c.getLink();

c.setLink(new Course(couseadded.getCourseName(),

couseadded.getSectionNumber(),

couseadded.getCredits(), null));

//IS EMPTY METHOD.

public boolean isEmpty()

return course == null;

//DROPS THE COURSE.

public boolean dropCourse(Course dropC)

Course current = course;

Course prev = null;

boolean droppedCourse = false;

if(isEmpty())

throw new NoSuchElementException("No courses Enrolled.");


if(current.getCourseName().equals(dropC.getCourseName())

&& current.getSectionNumber() == dropC.getSectionNumber())

this.course = course.getLink();

droppedCourse = true;

while(current != null && !droppedCourse)

prev = current;

current = current.getLink();

if(current.getLink() == null &&

current.getCourseName().equals(dropC.getCourseName())

&& current.getSectionNumber() == dropC.getSectionNumber())

current = null;

prev.setLink(current);

droppedCourse = true;

else if(current.getCourseName().equals(dropC.getCourseName())

&& current.getSectionNumber() == dropC.getSectionNumber())

prev.setLink(current.getLink());

current = null;

droppedCourse = true;

if(droppedCourse)
System.out.println("Course has been dropped.");

else

System.out.println("this Course has not been found.");

return droppedCourse;

//TO STRING METHOD.

public String toString()

String s = "";

Course c = course;

s = s +"Student ID: " + String.valueOf(this.getID());

if(this.isEmpty())

s= s + " The student is not enrolled any courses.";

else

while(c != null)

s += c;

c = c.getLink();

return (s);

}
}

//DEFAULT CONSTRUCTOR.

public Students()

this.maxNumOfStudents = 5;

this.currentNumOfStudents = 0;

s = new Student[maxNumOfStudents];

//OVERLOADED CONSTRUCTOR

public Students(int max)

this.currentNumOfStudents = 0;

this.maxNumOfStudents = max;

s = new Student[max];

//OVERLOADED CONSTRUCTOR.

public Students(Students cStud)

this.currentNumOfStudents = cStud.currentNumOfStudents;

this.maxNumOfStudents = cStud.maxNumOfStudents;

s = cStud.s;

//SETS THE MAX NUMBER OF STUDENTS.

public void setMaxNumberOfStudents(int max)

this.maxNumOfStudents = max;

}
//ADD THE STUDENTS.

public void addStudent(int studentId)

s[currentNumOfStudents] = new Student(studentId);

currentNumOfStudents++;

//ADDS THE NEW COURSE.

public void addCourse(int studentsId, Course addedcourse)

for(int i = 0; i < maxNumOfStudents; i++)

if(s[i].getID() == studentsId)

s[i].addCourse(addedcourse);

//DROPS THE COURSE.

public void dropCourse(int studentid, Course droppedcourse)

for(int i = 0; i < maxNumOfStudents; i++)

if(s[i].getID() == studentid)

s[i].dropCourse(droppedcourse);

//TO STRING METHOD.


public String toString()

String numStuds = "Number of students: " + maxNumOfStudents + "\n";

for(int i = 0; i < maxNumOfStudents; i++)

numStuds = numStuds+ s[i] + "\n";

numStuds += "\n";

return numStuds;

}
/*

Dipen Patel

CSC 236

Lab-3B

*/

public class Course

/*

* Private variables for Link, courseSec

* courseName and class credit.

*/

private int courseSec;

private Course link;

private String courseName;

private int credit;

//Default Contructor

public Course()

courseName = "";

courseSec = 0;

credit = 0;

link = null;

//Overloaded Constructor.
public Course(String course, int sectionNum, int credits, Course link)

this.courseName = course;

this.courseSec = sectionNum;

this.credit = credits;

this.link = link;

//OverLoaded Constructor.

public Course(Course c)

this.courseName = c.courseName;

this.courseSec = c.courseSec;

this.credit = c.credit;

this.link = c.link;

//Set course Method

public void setCourseName(String courseName)

this.courseName = courseName;

//Set section method

public void setSectionNumber(int coursesec)

courseSec = coursesec;

//Set credits method.

public void setCredits(int coursecredit)


{

credit = coursecredit;

//set Link method

public void setLink(Course link)

this.link = link;

//Get course Method

public String getCourseName()

return courseName;

//get Section method

public int getSectionNumber()

return courseSec;

//get credits method.

public int getCredits()

return credit;

// get Link method.

public Course getLink()

return link;
}

//to String method.

public String toString()

String outputString = "";

outputString = "\nCourse: " + courseName + "\tSection : " + courseSec

+ "\t Credits: " + credit + "\n";

return outputString;

}
/*

Dipen Patel

CSC 236

Lab-3B

*/

import java.util.*;

public class StudentDemo

public static void main(String[] args)

//CREATES AN OBJECT OF SCANNER TYPE.

Scanner scn = new Scanner(System.in);

/*

* CREATES ALL THE VARIABLES FOR

* COURSENAME, SECTION, CREDIT,OPTIONS

*/

String courseName = "";

int section = 0;

int credit = 0;

int studentID = 0;

int options = 0;

// Create an object of student type.

Students s = new Students(3);


//Adds the students.

s.addStudent(1111);

s.addStudent(1234);

s.addStudent(2357);

/*Prints out the menu with the

* options entered by the user

* until the loop is not being terminated.

*/

while (options != 9)

System.out.println("Choose an option to continue\n");

System.out.println("1: Show all students.");

System.out.println("2: Add a course.");

System.out.println("3: Drop a course.");

System.out.println("9: Quite");

options = scn.nextInt();

if (options == 1) {

System.out.println(s.toString());

else if (options == 2) {

System.out.println("Add a Course");

System.out.print("Enter student ID to add a course: ");

studentID = scn.nextInt();

scn.nextLine();

System.out.print("Enter Course Name: ");


courseName = scn.nextLine();

System.out.print("Enter Section Number: ");

section = scn.nextInt();

System.out.print("Enter Credits: ");

credit = scn.nextInt();

if ((courseName == "CSC162") || (courseName == "CSC236"))

credit = 4;

if (courseName == "HIS101")

credit = 3;

Course c = new Course(courseName, section, credit, null);

s.addCourse(studentID, c);

System.out.println(s.toString());

else if (options == 3) {

System.out.println("Drop a course");

System.out.print("Enter student ID to drop a course: ");

studentID = scn.nextInt();

scn.nextLine();

System.out.print("Enter Course Name: ");

courseName = scn.nextLine();
if ((courseName == "CSC162") || (courseName == "CSC236"))

credit = 4;

if (courseName == "HIS101")

credit = 3;

Course sDrop = new Course(courseName, section, credit, null);

s.dropCourse(studentID, sDrop);

} else if (options == 9 ) {

System.exit(1);

System.out.println("Thank you.");

scn.close();

You might also like