Question

In: Computer Science

I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...

I'm getting an error for this code? it won't compile

import java.util.*;
import java.io.*;

public class Qup3 implements xxxxxlist {// implements interface
   // xxxxxlnk class variables
   // head is a pointer to beginning of rlinked list
   private node head;
   // no. of elements in the list
   // private int count;

// xxxxxlnk class constructor
   Qup3() {
       head = null;
       count = 0;
   } // end Dersop3 class constructor

   PrintStream prt = System.out;

   // class node
   private class node {
       // class node variables
       int data;
       node rlink;

       // class node constructor
       node(int x) {
           data = x;
           rlink = null;
       } // class node constructor
   } // end class node

   // isEmpty() returns true if list is empty, false otherwise.
   public boolean isEmpty() {
       return (count == 0);
   } // end isEmpty

   // length() returns number of list elements.
   public int length() {
       return count;
   } // end length

   // insert x at position p, for successful insertion:
   // list should not be full and 1 <= p <= count+1
   public int insert(int x, int p) {
       int i;
       prt.printf("\n Insert %4d at position %2d:", x, p);
      
       if (p < 1 || p > ( count + 1) ) {
           return 0;
       }
       node tmp = new node(x);
       // p == 1 Inserts x to front of list,
       // is a special case where head changes
       if (p == 1) {
           tmp.rlink = head;
           head = tmp;
       } else {// traverse the list till element before p
           node current = head;
           // Find node before p
           for (i = 2; i < p; i++, current = current.rlink)
               ; // end for
           // insert node after cur node
           tmp.rlink = current.rlink;
           current.rlink = tmp;
       }
       count++;
       return 1; // successful insertion
   } // end insert

   // delete x at position p, for successful deletion:
   // list should not be empty and 1 <= p <= count
   public int delete(int p) {
       prt.printf("\n Delete element at position %2d:", p);
       if (isEmpty() || p < 1 || p > length())
           return 0; // invalid deletion
       int count = length();
       node tmp = head;
       // p == 1 deletes front of list.
       // This is a special case where head changes
       if (p == 1) { // Delete Front of List
           head = head.rlink;
           tmp.rlink = null;
           } else { // Find node before p
           node current = head;
           for (int i = 2; i < p; i++, current = current.rlink; ) ;// end for
           // Delete node after current node
           tmp = current.rlink;
           current.rlink = tmp.rlink;
           tmp.rlink = null; // delete tmp;
           }
       count--;
       return 1; // successful deletion
   } // end delete

   // sequential serach for x in the list
   // if successful return position of x in the
   // list otherwise return 0;
   public int searchx(int x) {
       prt.printf("\n search for %4d:", x);
       // complete the rest
       //.........
       return 0;
   } // end searchx

   // print list elements formatted
   public void printlist() {
       prt.print("\n List contents: ");
       for (node current = head; current != null; current = current.rlink)
           prt.printf("%4d, ", current.data);
      
       // end for
   } // end printlist

   public static void main(String args[]) throws Exception {
       int j, m, k, p, x, s;
       try {
           // open input file
           Scanner inf = new Scanner(System.in);
           // Create a List of type Integer of size n
           Qup3 lst = new Qup3();

           // read no. of elements to insert
           m = inf.nextInt();
           System.out.printf("\n\tInsert %2d elements in the list.", m);
           for (j = 1; j <= m; j++) {
               x = inf.nextInt(); // read x
               p = inf.nextInt(); // read position

               s = lst.insert(x, p); // insert x at position p
               if (s == 1)
                   System.out.printf(" Successful insertion.");
               else
                   System.out.printf(" %2d is invalid position for insertion.", p);
           } // end for
           lst.printlist(); // print linked list elements
           // read no. of elements to search in the list
           m = inf.nextInt();
           System.out.printf("\n\tSearch for %d elements in the list.", m);
           for (j = 1; j <= m; j++) {
               x = inf.nextInt(); // read x
               p = lst.searchx(x); // search for x
               if (p > 0)
                   System.out.printf(" found at position %d.", p);
               else
                   System.out.printf(" is not found.");
           } // end for
               // read no. of positions to delete from list
           m = inf.nextInt();
           System.out.printf("\n\tDelete %d elements from list.", m);
           for (j = 1; j <= m; j++) {
               p = inf.nextInt(); // read position
               s = lst.delete(p); // delete position p
               if (s == 1)
                   System.out.printf(" Successful deletion.");
               else
                   System.out.printf(" %2d is invalid position for deletion.", p);
           } // end for
           lst.printlist(); // print array elements
           inf.close(); // close input file
      
       } catch (Exception e) {
           System.out.print("\nException " + e + "\n");
       }
   System.out.print("\tAuthor: G. Dastghaibyfard \tDate: " +

java.time.LocalDate.now());} // end main
} // end class xxxxxlnk

Solutions

Expert Solution

Now, this code compiles fine.

There were 2 errors in the belowcode.

1.You have used the class variable 'count' in multiple places, but the declaration statement 'private int count;' was commented.

2.your class 'Qup3' implements the interface xxxxxlist. but xxxxxlist was not there in the code. If you have it with you, then it is fine. else please create the interface like below in a separate file with xxxxxlist as the file name.

public interface xxxxxlist {

}

If you are using a text editor, then it is better to put the interface and the class in the same folder.

These are the errors in the code that prevented it from compiling successfully. Now the above code compiles and runs fine.


import java.util.*;
import java.io.*;

public class Qup3 implements xxxxxlist {// implements interface
   // xxxxxlnk class variables
   // head is a pointer to beginning of rlinked list
   private node head;
   // no. of elements in the list
    private int count;

// xxxxxlnk class constructor
   Qup3() {
       head = null;
       count = 0;
   } // end Dersop3 class constructor

   PrintStream prt = System.out;

   // class node
   private class node {
       // class node variables
       int data;
       node rlink;

       // class node constructor
       node(int x) {
           data = x;
           rlink = null;
       } // class node constructor
   } // end class node

   // isEmpty() returns true if list is empty, false otherwise.
   public boolean isEmpty() {
       return (count == 0);
   } // end isEmpty

   // length() returns number of list elements.
   public int length() {
       return count; 
   } // end length

   // insert x at position p, for successful insertion:
   // list should not be full and 1 <= p <= count+1
   public int insert(int x, int p) {
       int i;
       prt.printf("\n Insert %4d at position %2d:", x, p);
       
       if (p < 1 || p > ( count + 1) ) {
           return 0;
       }
       node tmp = new node(x);
       // p == 1 Inserts x to front of list,
       // is a special case where head changes
       if (p == 1) {
           tmp.rlink = head;
           head = tmp;
       } else {// traverse the list till element before p
           node current = head;
           // Find node before p
           for (i = 2; i < p; i++, current = current.rlink)
               ; // end for
           // insert node after cur node
           tmp.rlink = current.rlink;
           current.rlink = tmp;
       }
       count++;
       return 1; // successful insertion
   } // end insert

   // delete x at position p, for successful deletion:
   // list should not be empty and 1 <= p <= count
   public int delete(int p) {
       prt.printf("\n Delete element at position %2d:", p);
       if (isEmpty() || p < 1 || p > length())
           return 0; // invalid deletion
       int count = length();
       node tmp = head;
       // p == 1 deletes front of list.
       // This is a special case where head changes
       if (p == 1) { // Delete Front of List
           head = head.rlink;
           tmp.rlink = null; 
           } else { // Find node before p
           node current = head;
           for (int i = 2; i < p; i++, current = current.rlink) ;// end for
           // Delete node after current node
           tmp = current.rlink;
           current.rlink = tmp.rlink;
           tmp.rlink = null; // delete tmp;
           }
       count--;
       return 1; // successful deletion
   } // end delete

   // sequential serach for x in the list
   // if successful return position of x in the
   // list otherwise return 0;
   public int searchx(int x) {
       prt.printf("\n search for %4d:", x);
       // complete the rest 
       //.........
       return 0;
   } // end searchx

   // print list elements formatted
   public void printlist() {
       prt.print("\n List contents: ");
       for (node current = head; current != null; current = current.rlink)
           prt.printf("%4d, ", current.data); 
       
       // end for
   } // end printlist

   public static void main(String args[]) throws Exception {
       int j, m, k, p, x, s;
       try {
           // open input file
           Scanner inf = new Scanner(System.in);
           // Create a List of type Integer of size n
           Qup3 lst = new Qup3();

           // read no. of elements to insert
           m = inf.nextInt();
           System.out.printf("\n\tInsert %2d elements in the list.", m);
           for (j = 1; j <= m; j++) {
               x = inf.nextInt(); // read x
               p = inf.nextInt(); // read position

               s = lst.insert(x, p); // insert x at position p
               if (s == 1)
                   System.out.printf(" Successful insertion.");
               else
                   System.out.printf(" %2d is invalid position for insertion.", p);
           } // end for
           lst.printlist(); // print linked list elements
           // read no. of elements to search in the list
           m = inf.nextInt();
           System.out.printf("\n\tSearch for %d elements in the list.", m);
           for (j = 1; j <= m; j++) {
               x = inf.nextInt(); // read x
               p = lst.searchx(x); // search for x
               if (p > 0)
                   System.out.printf(" found at position %d.", p);
               else
                   System.out.printf(" is not found.");
           } // end for
               // read no. of positions to delete from list
           m = inf.nextInt();
           System.out.printf("\n\tDelete %d elements from list.", m);
           for (j = 1; j <= m; j++) {
               p = inf.nextInt(); // read position
               s = lst.delete(p); // delete position p
               if (s == 1)
                   System.out.printf(" Successful deletion.");
               else
                   System.out.printf(" %2d is invalid position for deletion.", p);
           } // end for
           lst.printlist(); // print array elements
           inf.close(); // close input file
       
       } catch (Exception e) {
           System.out.print("\nException " + e + "\n");
       }
   System.out.print("\tAuthor: G. Dastghaibyfard \tDate: " +

java.time.LocalDate.now());} // end main
} // end class xxxxxlnk


Related Solutions

Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public class Number{ public static void main(String[] args) {    Scanner scan = new Scanner(System.in); System.out.println("Enter 20 integers ranging from -999 to 999 : "); //print statement int[] array = new int[20]; //array of size 20 for(int i=0;i<20;i++){ array[i] = scan.nextInt(); //user input if(array[i]<-999 || array[i]>999){ //check if value is inside the range System.out.println("Please enter a number between -999 to 999"); i--; } } //...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public static void main (String args []) { System.out.println("File:../SmallAreaIncomePovertyEstData.text"); System.out.println("Id" + "\t" + "Population" + "\t" + "ChildPop" + "\t" + "CPovPop" + "\t" + "CPovPop%"); }// read the data try (FileReader fr = new FileReader("File: C:\\605.201/SmallAreaIncomePovertyEstData.text")) { int c; while (( c = fr.read())!= -1){ System.out.print((char) c); } } catch(IOException e) { System.out.println("I/O Error" + e); }    } Please help to fix
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT