Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › To practise using methods in the String class of JDK.
New Posts  All Forums:Forum Nav:

To practise using methods in the String class of JDK.

post #1 of 4
Thread Starter 
Objective: To practise using methods in the String class of JDK.
Steps:

(i) Rewrite the GPA class code of Chapter 6 to use multiple (one or more) command line arguments, each consisting of a Course ID concatenated with a grade (such as: "CS101A" meaning Course ID: "CS101" and grade "A"). Create a new folder called 'assign52' under C:\Java\Lesson5 and copy GPA.java from Chapter 6 into it.
(ii) Write a new static method called 'gradePoints()' that takes in a single grade as a character variable, uses a 'switch' statement and returns the corresponding grade points using the following table:

Grade Grade Points
A 4
B 3
C 2
D 1
F 0

(iii) Write a new static method called parseInput that takes 3 parameters: an array of String called 'courseIdGrades', an array of String called 'courseIds', and an array of char called 'grades', and returns nothing (void). So the method header would look like this:


static void parseInput(String[] courseIdGrades, String[] courseIds, char[] grades)
{
...
}


Here 'courseIdGrades' represents the original String array of command line arguments supplied. 'courseIds' represents the array of just the Course ID's extracted from the input, and 'grades' represents the array of grades extracted from the input.

For example, if you run the program as:

java GPA CS101A CS102B EE103A EE201A

then:

• courseIdGrades will contain the values: "CS101A", "CS102B", "EE103A", "EE201A"
• 'courseIds' should contain the values: "CS101", "CS102", "EE103", "EE201", and
• 'grades' should contain 'A', 'B', 'A' and 'A'.

Note: The program should work for lower-case or upper-case input values or a mixture of the two.

Inside the body of the method:

• Next, use a while loop to iterate over courseIdGrades array (DO NOT use a break statement. Hint: You will need an 'int' variable that increments in each loop and check if it is less than the length of the 'courseIdGrades' array.)
• In each iteration, assume input is supplied correctly. Set the element of the 'courseIds' and 'grades' array with the courseId part and grade part extracted from 'courseIdGrade' using substring/charAt methods respectively. While parsing the input into the 'courseIds' array, convert to its upper-case equivalent by using the toUpperCase() method in the String type (so for example, an input of "cs101a" would become "CS101" in the courseIds array). While extracting the grade, call Character.toUpperCase() method on it and assign the result to the element of 'grades' array. The Character.toUpperCase() method that takes a single character and returns its upper-case equivalent. To see its exact syntax, look it up in the JDK 1.6 API available at:

JDK 1.7 API



(iv) Modify the signature of computeGPA() method to take an array of char called 'grades' (instead of array of int), and also change the method body to loop over each character of the array of grades, calling the 'gradePoints()' method each time to get the equivalent points.
(v) Write another static method called displayResult with return type 'void', that takes three parameters: the String array 'courseIds', char array 'grades' and a double called 'gpa' (in that sequence). So the method header would look like this:


static void displayResult(String[] courseIds, char[] grades, double gpa)
{
...
}


This method needs to display the list of original course ID's and the grades by looping over both arrays in a single 'for' statement. A sample output seesion is shown below:


C:\Java\Lesson5\assign52>javac GPA.java

C:\Java\Lesson5\assign52>java GPA
Please supply grades to find GPA

C:\Java\Lesson5\assign52>java GPA cs101B Ee102a

Course Grade
CS101 B
EE102 A
GPA: 3.5

C:\Java\Lesson5\assign52>java GPA CS101A CS102B EE103A EE201A

Course Grade
CS101 A
CS102 B
EE103 A
EE201 A
GPA: 3.75
C:\Java\Lesson5\assign52>

(vi) Go back to the main() method, remove existing code in it, and replace with the following.

• First check if at least one argument has been supplied. If not, show an error "Please supply grades to find GPA" and exit.
• If at least one argument has been supplied, do the following.
• Declare a String array type variable called 'courseIds' and allocate memory for it using the "new" keyword. It needs to have as many elements as the number of run-time arguments supplied. In other words, the array has a variable number of elements, the exact number known only after the user supplies command line arguments.
• Declare a char array type variable called 'grades' and allocate memory for it using "new" keyword (similar to the way you did for 'courseIds').
• Call parseInput sending it appropriate arguments.
• Call displayResult passing it appropriate arguments. The 'gpa' argument needs to be the value returned from a call to 'computeGPA'. In other words, it should be a function call within another function call. DO NOT call displayResults and computeGPA on individual statements.

(vii) Submit GPA.java.
post #2 of 4
And?
At least post some degree of effort before dumping off your coursework.
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
post #3 of 4
I looked around a bit, it seems this is just a bot pulling random java questions off of google and dumping them here. I'm not sure why anyone would do that though. headscratch.gif
The Clunker
(11 items)
 
Lenovo Y40
(6 items)
 
 
CPUMotherboardGraphicsRAM
Core 2 Quad Q8400 Asus P5P43TD Powercolor 7950 4GB Patriot Viper 
Hard DriveHard DriveCoolingOS
80GB WD Caviar Blue 500GB WD Caviar Black Coolermaster Hyper TX Windows 7 Home Premium 64-bit 
MonitorPowerCase
20" Samsung Corsair CX600M Antec 300 
CPUGraphicsRAMHard Drive
i7-4510U R9 M275 2GB 8GB @ 1600MHz 500+8GB 5400rpm SSHD 
OSMonitor
Windows 8.1 14" 1080p 
  hide details  
Reply
The Clunker
(11 items)
 
Lenovo Y40
(6 items)
 
 
CPUMotherboardGraphicsRAM
Core 2 Quad Q8400 Asus P5P43TD Powercolor 7950 4GB Patriot Viper 
Hard DriveHard DriveCoolingOS
80GB WD Caviar Blue 500GB WD Caviar Black Coolermaster Hyper TX Windows 7 Home Premium 64-bit 
MonitorPowerCase
20" Samsung Corsair CX600M Antec 300 
CPUGraphicsRAMHard Drive
i7-4510U R9 M275 2GB 8GB @ 1600MHz 500+8GB 5400rpm SSHD 
OSMonitor
Windows 8.1 14" 1080p 
  hide details  
Reply
post #4 of 4
Thread Starter 
class GPA
{
public static void main(String[] args)
{
if (args.length == 0)
{
System.out.println("Please supply grades to find GPA");
System.exit(0);
}
else
{
String[] courseIds = new String[args.length];
char[] grades = new char[args.length];
parseInput(args, courseIds, grades);
displayResult(courseIds, grades, computeGPA(grades));
}
}

static void parseInput(String[] courseIdGrades, String[] courseIds, char[] grades)
{
int i = 0;

while( i < courseIdGrades.length )
{
courseIds = courseIdGrades.substring(0,5).toUpperCase();
grades = Character.toUpperCase(courseIdGrades.charAt(5));
i++;
}
}

static double computeGPA(char[] grades)
{
int sum = 0;
for (int i = 0; i < grades.length; i++)
{
sum += gradePoints(grades);
}
double gpa = (double) sum / grades.length;
return gpa;
}

static int gradePoints(char grade)
{
int points = 0;
switch(grade)
{
case 'A':
points = 4;
break;
case 'B':
points = 3;
break;
case 'C':
points = 2;
break;
case 'D':
points = 1;
break;
default:
points = 0;
break;
}
return points;
}

static void displayResult(String[] courseIds, char[] grades, double gpa)
{
System.out.println("

");
System.out.println(" Course ID Grade" );
System.out.println("
");
for (int i = 0; i < courseIds.length; i++)
{
System.out.println(" " + courseIds + " " + grades);
}
System.out.println("
");
System.out.println(" GPA = " + gpa);
System.out.println("
");
}
}
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › To practise using methods in the String class of JDK.