You need to do these questions Thank you. This is for operating systems. Program
ID: 3740597 • Letter: Y
Question
You need to do these questionsThank you. This is for operating systems. Program1 import java.util.* public class Overflow ( static final int INPUT SIZE-10; public static void main(Stringt] args) ( char[] vals new char[INPUT SIZE] scanner scan new scanner (System.in); String s1 getstring (scan) copyVals(s1,vals): string sub getSubstring(scan,vals): system.out.println( sub string: +sub) public static String getstring(Scanner scan) t System.out.print("Please type a string:"a string s scan.nextLine() return S public ??tatic void copyvais (String ?,char[] vals) { for (int í-o; î
Explanation / Answer
import java.util.*;
public class Overflow {
static final int INPUT_SIZE=10;
public static void main(String[] args)
{
char[] vals=new char[INPUT_SIZE]; //Array declaration
Scanner scan=new Scanner(System.in);
String s1=getString(scan);
copyVals(s1,vals);
String sub=getSubstring(scan,vals);
System.out.println("sub string : "+sub);
}
public static String getString(Scanner scan)
{
System.out.print("Please type a string : ");
String s=scan.nextLine();
return s;
}
public static void copyVals(String s, char[] vals)
{
for(int i=0;i<s.length();i++) //Array index is i and 0<=i<length of string s
vals[i]=s.charAt(i); //Occurrence of index variable i
}
public static String getSubstring(Scanner scan,char[] vals)
{
System.out.print("Starting Point : ");
int start =scan.nextInt();
System.out.print("Ending Point : ");
int end =scan.nextInt();
char[] newChars=getChars(start,end,vals); //Array declaration
return new String(newChars);
}
public static char[] getChars(int start, int end, char[] vals)
{
int sz=end-start;
char[] result=new char[sz]; //Array declaration
for(int i=0;i<sz;i++) //Array index is I and 0<=i<sz
{
result[i]=vals[start+i]; //Occurrence of index variable i
}
return result;
}
}
2.3
V
for(int i=0;i<s.length();i++)
Here i is initialized to 0. Then in each iteration the value of I is incremented by 1, until i is less than length of string s.
for(int i=0;i<sz;i++)
Here i is initialized to 0. Then in each iteration the value of I is incremented by 1, until i is less than sz (which is end –start).
3.1
for(int i=0;i<s.length();i++)
for(int i=0;i<sz;i++)
3.2
for(int i=0;i<s.length();i++)
vals[i]=s.charAt(i); // Array limit of vals=10
Here if size of string s is greater than 10 then the loop limit could exceed the legal range of array index.
for(int i=0;i<sz;i++)
result[i]=vals[start+i];
Here also sz=end-start. If this value is greater than 10, then the loop limit could exceed the legal range of array index.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.