Please provide the full answers to question 2 & 3 (below)! 2. How many lexemes d
ID: 3746798 • Letter: P
Question
Please provide the full answers to question 2 & 3 (below)!2. How many lexemes does the following Java code contain?
1. public class RepeatString { 2. public static void main(String args[]) { 3. Scanner input = new Scanner(System.in); 4. System.out.println("Enter a string: "); 5. String phrase = input.nextLine(); 6. System.out.println("Enter # times to repeat string: "); 7. int repetitions = input.nextInt(); 8. repeat(phrase, repetitions); 9. } 10. public static void repeat(String str, int reps) { 11. for (int i = 1; i <= reps; i++) { 12. System.out.println(i + ". Repeat: " + str); 13. } 14. } }
Give the number of lexemes on each line (using the line numbers shown) as well as the total number of lexemes.
3. Problem 17 in Chapter 1 of Sebesta: “Some programming languages --- for example, Pascal --- have used the semicolon to separate statements, while Java uses it to terminate statements. Which of these, in your opinion, is most natural and least likely to result in syntax errors? Support your answers.” 2. How many lexemes does the following Java code contain?
1. public class RepeatString { 2. public static void main(String args[]) { 3. Scanner input = new Scanner(System.in); 4. System.out.println("Enter a string: "); 5. String phrase = input.nextLine(); 6. System.out.println("Enter # times to repeat string: "); 7. int repetitions = input.nextInt(); 8. repeat(phrase, repetitions); 9. } 10. public static void repeat(String str, int reps) { 11. for (int i = 1; i <= reps; i++) { 12. System.out.println(i + ". Repeat: " + str); 13. } 14. } }
Give the number of lexemes on each line (using the line numbers shown) as well as the total number of lexemes.
3. Problem 17 in Chapter 1 of Sebesta: “Some programming languages --- for example, Pascal --- have used the semicolon to separate statements, while Java uses it to terminate statements. Which of these, in your opinion, is most natural and least likely to result in syntax errors? Support your answers.” 2. How many lexemes does the following Java code contain? 1. public class RepeatString t 2 public static void main(String args[]) Scanner input = new Scanner (System.in ); System.out.println("Enter a string: " String phrase-input.nextLine) System.out.print 1n ("Enter # times to repeat string: "); int repetitionsinput.nextInt) repeat (phrase, repetitions); 10. public static void repeat (String str, int reps) for (int i = 1;
Explanation / Answer
2)public class RepeatString {//lexemes-4
public static void main(String args[]) {//lexemes-11
Scanner input = new Scanner(System.in);//lexemes-11
System.out.println("Enter a string: ");//lexemes-14
String phrase = input.nextLine();//lexemes-9
System.out.println("Enter # times to repeat string: ");//lexemes-17
int repetitions = input.nextInt();//lexemes-9
repeat(phrase, repetitions);//lexemes-7
}//lexemes-1
public static void repeat(String str, int reps) {//lexemes-12
for (int i = 1; i <= reps; i++) {//lexemes-17
System.out.println(i + ". Repeat: " + str);//lexemes-19
}//lexemes-1
} //lexemes-1
}//lexemes-1
3)I feel java's way of using semicolon to ternimate statements is natural and least likely to cause syntax errors because almost everyone starts coding with c language and the basic syntax learnt there is usage of ; for breaking statements.So this makes everyone to naturally use ; a lot unlike in pascal where it is used for some other purpose and moreover irrespective of language it has become a habit for everyone to use ; by default .So clearly java is developer friendly.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.