如何在我的main方法中测试它们,我如何正确实现我的四个方法操作? [英] How can I implement my four method operations right in order to test them in my main method?

查看:75
本文介绍了如何在我的main方法中测试它们,我如何正确实现我的四个方法操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把我的java程序分成两类;一个叫做Student类,另一个叫做StudentListing类。我只想弄清楚如何正确地实现四种方法操作,所以我可以在main方法中使用它们进行测试。有人可以帮忙吗?我在挣扎。这是我的两个类的代码。



I have broken my java program down into two classes; one is called a Student class and the other class is called an StudentListing class. I am just trying to figure out how I can correctly implement the four method operations right, so I can use them in the main method for testing purposes. Can someone please help? I am struggling. Here is my code for both classes.

public class Student{
           
     private String name;   // key field 

     private int ID; 

     private double GPA; 


   public Student(String name, int ID, double GPA){
     
       this.name= name; 
       this.ID= ID; 
       this.GPA=GPA; 
   } 

    public void setName(String name){ 
    
     this.name= name; 

   } 

   public String toString() { 
 
      return " Student Name: " + name + " Student ID: " + ID + "Student GPA: " + GPA; 
    }
  
  public String getName() { 

      return name; 

    }

   public void setID(int ID) { 

     this.ID= ID; 
 
    } 

     public int getID(){ 

      return ID; 

    } 

    public void setGPA(double GPA){ 

       this.GPA= GPA; 

    }

   public double getGPA(){ 
 
      return GPA; 

   }

  
}










public class StudentListings{


private Student[] data; // an array of Student objects
private int next;
private int size;


 
public StudentListings(){ 

  next=0; 
  size=100;
  data= new Student[size];

} 

public StudentListings(int s){ 

   next=0;
   data= new Student[s];
   size=s;

 } 

  public boolean insert(Student newStudentListing) { 

     // the structure is full 

    if(next > = size)  
      return false; 
 
    // store a deep copy of the client's node
 
     data[next]= newStudentListing.deepCopy(); 
 
     if(data[next]== null) 
       return false; 
      next= next + 1// prepare for the next insert 
       return true; 
   } 


  public StudentListings fetch(String targetKey){ 
    
      
    StudentListings student; 
    StudentListings temp; 
 
    // access the node using a sequential search 
     int i=0; 
 
    while(i < next &&!(data[i].compareTo(targetKey)==0)
    {
        i++; 
    } 
 
    if(i== next) // node not found
      return null; 
 
     // deep copy the node's information into the client's node 
 
      student= data[i].deepCopy(); 
 
     if(i!= 0// bubble-up accessed node
     { 
        temp= data[i-1]; 
        data[i-1]=data[i];
        data[i]= temp;
     } 
        return student; 
   }

   public boolean delete(String targetKey){ 
    
     int i=0; 
    while(i < next && !(data[i].compareTo(targetKey)==0))
     { 
        i++; 
     } 
     if(i==next) // node not found
 
      // move the last node into the deleted node's position
       return false; 
       data[i]= data[next-1]; 
       data[next-1]=null; 
       next= next-1; 
       return true; // node found and deleted 

   }

 public boolean update(String targetKey, Student newStudentListing){ 

   if(delete(targetKey)== false)
      return false; 
    else if(insert(newStudentListing)==false) 
      return false; 
    else 
       return true; 
} 


   public void showAll(){ 

   for(int i=0; i< next; i++){ 

   System.out.println(data[i].toString());
  
  } 


public static void main(String[] args){ 

      StudentListings obj1= new StudentListings();
      
      Student l1 = new Student("Terrence", 1, 3.45);
      Student l2 = new Student("Roberta", 2, 2.15);
      Student l3 = new Student("George", 3, 1.50);
      
       obj1.insert(l1);
       obj1.insert(l2);
       obj1.insert(l3);

       obj1.ShowAll();

       l3= obj1.fetch("Terrence");

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

       obj1.delete("Roberta");

       obj1.ShowAll();

       obj1.update("George");

       obj1.ShowAll(); 

}





我的尝试:



我试图在我的主要方法中实现这些方法,但尚未对它们进行测试,因为我需要一些帮助才能首先实现它们。有人可以帮助我吗?



What I have tried:

I have tried to implement these methods in my main method which above but haven't tested them yet because I need some help with implementing them right first. Can someone help me?

推荐答案

package mypackage;

public class Student {

	private String name; // key field
	private int ID;
	private double GPA;
	
	public Student(String name, int ID, double GPA) {
		this.name = name;
		this.ID = ID;
		this.GPA = GPA;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String toString() {
		return " Student Name: " + name + " Student ID: " + ID + " Student GPA: " + GPA;
	}
	
	public String getName() {
		return name;
	}
	
	public void setID(int ID) { 		  
		this.ID= ID;		 
	} 
		 
	public int getID(){		 
	    return ID;		 
	} 
		 
	public void setGPA(double GPA){		 
		this.GPA= GPA; 		 
    }
	
	public double getGPA(){ 	   
	    return GPA;		 
	}
	
}



package mypackage;

public class StudentListings {

	// https://www.codeproject.com/Questions/1207978/How-can-I-implement-my-four-method-operations-righ

	public static void main(String[] args) {

		StudentListings obj1 = new StudentListings();

		// NOTE: would stick to avoid using numbers as object names
		//       this will get messed up if you want to pass them into
		//       a method as an argument.
		Student l1 = new Student("Terrence", 1, 3.45);
		Student l2 = new Student("Roberta", 2, 2.15);
		Student l3 = new Student("George", 3, 1.50);
		
		Student kid14 = new Student("John", 4, 2.36);

		obj1.insert(l1);
		obj1.insert(l2);
		obj1.insert(l3);

		// ERROR: Case sensitive. ShowAll needs to be showAll.
		// obj1.ShowAll();
		obj1.showAll();

		//this call looks good
		l3 = obj1.fetch("Terrence");
		
		System.out.println(l3.toString());

		//this call looks good
		obj1.delete("Roberta");

		//this call looks good
		obj1.showAll();

		// here you are missing the second argument to your method
		obj1.update("George", kid14);
		
		//this call looks good
		obj1.showAll();

	}

	private Student[] data; // an array of Student objects
	private int next;
	private int size;

	public StudentListings() {

		next = 0;
		size = 100;
		data = new Student[size];

	}

	public StudentListings(int s) {
		next = 0;
		data = new Student[s];
		size = s;
	}

	public boolean insert(Student newStudentListing) {
		// the structure is full
		if (next >= size) {
			return false;
		} else {
			data[next] = newStudentListing;
			next = next + 1; // prepare for the next insert
			return true;
		}
	}

	// ERROR: you are trying to assign an object of StudentListings type to a
	// Student object
	// Public StudentListings fetch(String TargetKey) {
	public Student fetch(String TargetKey) {
		// StudentListings student;
		// StudentListings temp
		Student student;

		// Access the node using a sequential search
		//ERROR: not needed
		// int i=0;

		// ERROR: why use a while here? Should be a for loop to iterate sequentially
		// through your array.
		// (I'm guessing) the if the name in data[i] compares to target key that is the
		// student
		// we want to return
		// while(i < next &&!(data[i].compareTo(targetKey)==0)
		for (int i = 0; i <= next; i++) {
			// if data item == target
			if (data[i].getName() == TargetKey) {
				student = data[i];
				return student;
			}
		}
		// if target does not exist and nothing is retrieved return null
		return null;
		
		// not sure what all this is, but its too much.
	   /* if(i== next) // node not found
	        return null; 
	   
	       // deep copy the node's information into the client's node 
	   
	        student= data[i].deepCopy(); 
	   
	       if(i!= 0) // bubble-up accessed node
	       { 
	          temp= data[i-1]; 
	          data[i-1]=data[i];
	          data[i]= temp;
	       } 
	          return student; 
	     }*/
	}

	public boolean delete(String targetKey) {
		boolean deleted = false;
		// if you want to use while loops in your functions you should separate the second condition 
		// to another if statement within it like this:
		int i = 0;
		// 2 conditions less than count and if we haven't deleted entry yet
		while (i < next || deleted == true) {
			//put the condition here, so if we find something we can return it 
			if(data[i].getName() ==  targetKey) {				
				
				//This is where you will put your deletion logic.. I'll leave that to you
				
			}
			i++;
		}
		
		//don't need this if statement
		//if (i == next) // node not found

			// move the last node into the deleted node's position
			//return false;
		//looks like you are attempting to delete the previous record out of the array here but idk.
		//either way it is not needed.
		//data[i] = data[next - 1];
		//data[next - 1] = null;
		next = next - 1;
		//return deleted instead of true. If it did delete something it will be true, else false
		return deleted; // node found and deleted 
	}


	public boolean update(String targetKey, Student newStudentListing) {
		if (delete(targetKey) == false)
			return false;
		else if (insert(newStudentListing) == false)
			return false;
		else
			return true;
	}

	public void showAll() {
		for (int i = 0; i < next; i++) {
			System.out.println(data[i].getName() + ", " + data[i].getID() + ", " + data[i].getGPA());
		}
	}
}


这篇关于如何在我的main方法中测试它们,我如何正确实现我的四个方法操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆