Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
9.5 kB
1
Indexable
Never
import java.util.Scanner;

public class Main {

    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {


		/*

		System.out.println("Enter the size of array: ");

		int size = scan.nextInt();

		int [] arr = new int[size];

		System.out.println("Enter the numbers: ");

		for(int i =0; i<arr.length; i++){
			arr[i] = scan.nextInt();
		}

		int [] insertarray = new int[size +1];
		System.out.println("Enter the position to insert the number: ");
		int position = scan.nextInt();
		System.out.println("Enter the number you like: ");
		int number = scan.nextInt();

		for (int i = 0; i<arr.length+1; i++){

			if(i < position){
				insertarray[i] = arr[i];
			}
			else if (i == position){
				insertarray [i] = number;
			}
			else{
				insertarray[i] = arr[i-1];
			}
		}

		for (int i =0; i<arr.length+1;i++){
			System.out.println(insertarray[i]);
		}


		//int [] deletearray = new int[size+1];
		 int [] deletearray = insertarray;

		System.out.println("Enter the position index where you want to remove the number: ");
		position = scan.nextInt();

	   for(int i =0; i<insertarray.length; i++){
		   if(i < position){
			  deletearray[i] = insertarray[i];
		   }
		   else if (i == position){
			   continue;
		   }
		   else {
			   deletearray[i-1]= insertarray[i];
		   }
	   }
	   for(int i = 0; i<insertarray.length-1; i++){
		   System.out.println(deletearray[i]);
	   }
	   */
		/*
        System.out.println("Enter the size of array: ");

		int size = scan.nextInt();

		int [] arr = new int[size];

		System.out.println("Enter the numbers: ");

		for(int i =0; i<arr.length; i++){
			arr[i] = scan.nextInt();
		}

		int pos =0, num= 0;
		arr = insert(arr, pos, num);
		display(arr);

		arr = remove(arr, pos);
		display(arr);

	}
	////////////// insert method array ////////////////////////////////////
	public static int[] insert(int [] arr, int position, int number ){

		System.out.println("In which position you want to enter the number: ");
		position = scan.nextInt();

		System.out.println("Which number you want to enter: ");
		number = scan.nextInt();

		int [] newarray = new int[arr.length+1];

		for(int i=0; i<newarray.length;i++){

			if(i<position){
				newarray[i] = arr[i];
			}
			else if (i == position){
				newarray[position]  = number;
			}
			else {
				newarray[i] = arr[i-1];
			}
		}
		return newarray;
	}
	 //// remove method array //////////////////////////////////////////////////////
	public static int [] remove(int[] arr, int position){

		System.out.println("Enter the position in order to remove the number: ");
		position = scan.nextInt();

		int [] deletearray = new int[arr.length-1];
		for(int i =0,j=0; i<arr.length; i++){

			if(i==position){
				continue;
			}
			else {
				deletearray[j++] = arr[i];
			}
		}
		return deletearray;

	}
	//////////display array method  ////////////////////////////
	public static void display(int [] arr){
		for(int i =0; i<arr.length; i++){
			System.out.println(arr[i]);
		}
		*/


        CourseCSC225 csc225 = new CourseCSC225("Data Structure");
        csc225.addStudent("alma",89);
        csc225.addStudent("john",98);
        csc225.addStudent("baez",77);
        csc225.addStudent("michael",88);

        ///   csc225.addStudent("james", 87);


        String students1[] = csc225.getStudents();

        for(int i = 0; i<students1.length; i++){
            System.out.println("index is: " + (i)+ " --- > " + students1[i]);
        }

        ///// insert or add more students ./////////


        System.out.println("Enter the name of student: ");
        String student = scan.next();
        System.out.println("Enter the student grade: ");
        int grade1 = scan.nextInt();
        //System.out.println("Enter the student grade: ");
        //double grade1 = scan.nextDouble();

        csc225.addStudent(student,grade1);


        students1 = csc225.getStudents();

        for(int i =0; i<students1.length; i++){
            System.out.println("index is: " + (i)+ " --- > " + students1[i]);
        }
        /// remove student from the course///////
        System.out.println("Enter the name of student to remove: ");
        student = scan.next();

        csc225.dropStudent(student);

        students1=csc225.getStudents();

        for(int i =0; i<students1.length; i++){
            System.out.println("index is: " + (i)+ " --- > " + students1[i]);
        }
        System.out.println("Enter the name of student to remove: ");
        student = scan.next();
        csc225.dropStudent(student);
        students1=csc225.getStudents();

        for(int i =0; i<students1.length; i++){
            System.out.println("index is: " + (i)+ " --- > " + students1[i]);
        }
        //////////// insert by position //////////////////////////
        System.out.println("Enter the postion of index to add the student: ");
        int position = scan.nextInt();
        System.out.println("Enter the name of student: ");
        String insertname = scan.next();

        String arr[] = new String[students1.length+1];

        for (int i =0; i< arr.length;i++){
            if(i < position){
                arr[i]= students1[i];
            }
            else if (i == position){
                arr[position] = insertname;
            }
            else{
                arr[i] = students1[i-1];
            }
        }

        System.out.println("----------------------\n");
        for(int i =0; i<arr.length; i++){
            System.out.println("index is: " + (i)+ " --- > " + arr[i]);
        }

        ///////////////remove student by index //////////////////////


        System.out.println("From which position you want to remove the student: ");

        int index = scan.nextInt();

        String [] removearray = new String[arr.length-1];

        for(int i =0, j=0; i<removearray.length;i++,j++){

            if (i == index){
                j++;
            }
            removearray[i]=arr[j];
        }

        System.out.println("--------------------------\n");
        for(int i =0; i<removearray.length;i++){
            System.out.println("index is: " + (i)+ " --- > " + removearray[i]);
        }
        ///////////////////////////////////////////////////////////////////////////
        insertArray(csc225);
        display(csc225);

    }


    public static void insertArray(CourseCSC225 asd){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter student name: ");
        String name = scan.next();
        System.out.println("Enter student grade1: ");
        int grade1 = scan.nextInt();
        asd.addStudent(name, grade1);
    }

    public static void display(CourseCSC225 asd){
        for(int i =0; i<asd.getStudents().length; i++){
            System.out.printf("%2d %7s %7d\n",i,asd.getStudents()[i], asd.getScore1()[i]);
        }
    }


}
//// end of main class //////////////////////////

////////////  suggestion how to include class instead of using all parameters one by one///////////
class Coursecsc22501{
    String name;
    int score1;
    int score2;

    // do the getters for each variable, too
}
/////////////////////////////////////////////////////////////////////////////////////////////
class CourseCSC225{

    private String courseName;
    private String [] students = new String[1];
    private int [] score1 = new int[1];
    private int numberOfStudents;

    public CourseCSC225(String courseName){
        this.courseName = courseName;
    }

    public String[] getStudents(){
        return students;
    }

    public int[] getScore1(){
        return score1;
    }

    public int getNumberOfStudents(){
        return numberOfStudents;
    }

    public String getCourseName(){
        return courseName;
    }

    ///////////////// add students method ///////////////////
    public void add(Coursecsc22501 student){

    }
    public void addStudent(String student, int grade1){

        String [] a = new String[students.length+1];

        int [] tempScore1 = new int[score1.length+1];

        if(numberOfStudents == students.length){
            for (int i = 0; i<numberOfStudents; i++){
                a[i] = students[i];
                tempScore1[i] = score1[i];
            }
            students = a;
            score1 = tempScore1;

        }
        students[numberOfStudents] = student;
        score1[numberOfStudents]= grade1;
        numberOfStudents++;
    }
    /////////////////////////////////////


    public void dropStudent(String student){
        int position = findStudent(student);
        if(position >=0){
            dropStudent(position);
        }
        else{
            System.out.println("The student doesnt exists in this course.. try again");
        }
    }
    private void dropStudent(int position){

        String [] s = new String[students.length -1];
        int [] removestudent = new int[score1.length-1];

        for(int i =0, j=0; i<s.length; i++,j++){
            if(i == position){
                j++;
            }
            s[i]=students[j];
            removestudent[i] = score1[j];
        }
        this.students=s;
        this.score1 = removestudent;
        numberOfStudents--;
    }
    //////////



    /////////


    private int findStudent(String student){
        for(int i =0; i<numberOfStudents; i++){
            if(students[i].equals(student)){
                return i;
            }
        }
        return -1;
    }




}