Lab 8
Choong Kar Yee
182630
1.
import java.util.Scanner;
public class Test1{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int i=1;
int noOfStudents=2;
while(i<= noOfStudents){
System.out.println("Enter first test score");
double test1=input.nextDouble();
System.out.println("Enter second test score");
double test2=input.nextDouble();
System.out.println("Enter labwork score");
double labwork=input.nextDouble();
System.out.println("Enter final exam score");
double exam=input.nextDouble();
double mark=test1+test2+labwork+exam;
System.out.println("Enter the mark" + mark);
System.out.println("Your grade is " + grade(mark));
i++;
}}
public static char grade(double mark){
char grade;
if (mark>=80)
{
grade='A';
}
else if (mark>=60)
{
grade='B';
}
else grade='F';
return grade;
}
}
public class Test1{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int i=1;
int noOfStudents=2;
while(i<= noOfStudents){
System.out.println("Enter first test score");
double test1=input.nextDouble();
System.out.println("Enter second test score");
double test2=input.nextDouble();
System.out.println("Enter labwork score");
double labwork=input.nextDouble();
System.out.println("Enter final exam score");
double exam=input.nextDouble();
double mark=test1+test2+labwork+exam;
System.out.println("Enter the mark" + mark);
System.out.println("Your grade is " + grade(mark));
i++;
}}
public static char grade(double mark){
char grade;
if (mark>=80)
{
grade='A';
}
else if (mark>=60)
{
grade='B';
}
else grade='F';
return grade;
}
}
2.
import java.util.Scanner;
public class Test2{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
//System.out.println("Enter a character");
//char a=input.next().CharAt(0);
System.out.println("Enter a String");
String str=input.nextLine();
System.out.println("The number of letters in the string \"" + str + "\": " + countLetters(str));
}
public static int countLetters(String str){
int count =0;
for (int i=0; i<str.length();i++)
{
if (Character.isLetter(str.charAt(i)))
count++;
}
return count;
}
}
import java.util.Scanner;
public class Test2_b {
/** Main Method */
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a Scanner
// Prompt the user to enter a string followed by a character
System.out.print(
"Enter a string followed by a character e.g. Welcome, e : " );
String string = input.nextLine();
// Extract character and substring
int k = string.indexOf(", ");
String str = string.substring(0, k);
char ch = string.charAt(k + 2);
// Display the nubmer of occurrences of the character in the string
System.out.println(
"The number of occurrences of \"" + ch + "\" in \'" + str + "\" is: " +
count(str, ch));
}
/** Method count */
public static int count(String str, char a) {
int count = 0; // Initialize count to 0
// Count the number of occurrences of the character a in the string str
for (int i = 0; i < str.length(); i++) {
if (a == str.charAt(i))
count++;
}
return count;
}
}
https://github.com/jsquared21/Intro-to-Java-Programming/blob/master/Exercise_06/Exercise_06_23/Exercise_06_23.java
No comments:
Post a Comment