I would like add GPA in my code but unfortunately not working - netbeans-8

I would ask if someone can help me the GPA in my code not working as per required.please find below it.
Scanner Student=new Scanner(System.in);
System.out.println("Please Enter Three Marks for Student");
int sum=0;
double avg;
int[]x=new int[3];
for (int i = 0; i < x.length; i++) {
System.out.println("The mark : "+ ( i +1));
x[i]=Student.nextInt();
sum=sum+x[i];
}
char grade;
if (sum >= 90 )
{
grade = 'A';
}
else if (sum >= 80 )
{
grade = 'B';
}
else if (sum >= 70)
{
grade = 'C';
}
else if (sum >= 50)
{
grade = 'D';
}
else
{
grade = 'F';
}
// Display the grade.
System.out.println("The grade is " + grade);
avg=sum/x.length;
System.out.println("The total of three marks are :"+sum+"\nThe Average is :"+avg);
}

Related

how to Print out the employee details having highest and second highest salary?

csv file which has 3 entries of empname,emp id,designation and salary Robert,33,Manager,12000 Duval,23,Associate,6000 Kierron,33,AD,20000
From below code how to Print out the employee details having the highest and second highest salary?
enter code here
public class HighSalary {
public static void highSalary() throws IOException {
String record;
BufferedReader br = new BufferedReader(new FileReader("/Users/ak/Documents/emp.csv"));
System.out.println("\t\t Printing Employee Details of Highest & Second Highest Salary \n");
System.out.println(" ------------------------------------------------------------- ");
System.out.println("| ID Name Age Address |");
System.out.println(" ------------------------------------------------------------- ");
List<List<String>> arlist = new ArrayList<>();
int highestSal = 0;
int secondSal = 0;
while ((record = br.readLine()) != null) {
String[] words = record.split(",");
arlist.add(Arrays.asList(words));
int salary = Integer.parseInt(words[3]);
if (highestSal == 0) {
highestSal = salary;
secondSal = salary;
}
else if (salary > highestSal) {
secondSal = highestSal;
highestSal = salary;
}
else if (salary > secondSal || secondSal == 0) {
secondSal = salary;
}
}
System.out.println("highest salary: " + highestSal);
System.out.println("second highest salary: " + secondSal);
br.close();
}
}
enter code here
You may iterate the file, keeping track of the highest and second highest salary. When assigning a new highest salary, the second highest should be assigned to the previous highest salary. Also, you may store two strings for the metadata associated with the highest and second highest paid employees.
Integer highest = null;
Integer second = null;
String highEmp = "";
String secondEmp = "";
while ((record = br.readLine()) != null) {
String[] words = record.split(",");
arlist.add(Arrays.asList(words));
int salary = Integer.parseInt(words[3]);
if (highest == null) {
highest = salary;
second = salary;
highEmp = record;
secondEmp = record;
}
else if (salary > highest) {
second = highest;
secondEmp = highEmp;
highest = salary;
highEmp = record;
}
else if (salary > second) {
second = salary;
secondEmp = record;
}
}
System.out.println("highest salary: " + highest);
String[] parts = highEmp.split(",");
System.out.println("Name: " + parts[0] + ", Age: " + parts[1] + ", Desig: " + parts[2]);
System.out.println("second highest salary: " + second);
parts = secondEmp.split(",");
System.out.println("Name: " + parts[0] + ", Age: " + parts[1] + ", Desig: " + parts[2]);

Need help on getting highest salary and second highest salary from emp.csv using java

I have one csv file which has 3 entries of empname,emp id,designation and salary
Robert,33,Manager,12000
Duval,23,Associate,6000
Kierron,33,AD,20000
Using Java program, I like to achieve the highest salary and second highest salary
public class HighSalary {
public static void highSalary() throws IOException {
String record;
BufferedReader br = new BufferedReader(new FileReader("/Users/ak/Documents/emp.csv"));
System.out.println("\t\t Max Salary Record\n");
System.out.println(" ------------------------------------------------------------- ");
System.out.println("| Name Age Desig Salary |");
System.out.println(" ------------------------------------------------------------- ");
List<List<String>> arlist = new ArrayList<>();
// List<String> list = Arrays.asList(a);
int maxSal = 0;
while ((record = br.readLine()) != null) {
String[] words = record.split(",");
arlist.add(Arrays.asList(words));
for (int i = 0; i < words.length; i++) {
if (maxSal <= Integer.parseInt(words[3])) {
maxSal = Integer.parseInt(words[3]);
} else {
maxSal = maxSal;
}
}
System.out.println("From The Salary Largest Number is:" + maxSal);
}
br.close();
}
}
the problem with this code is printing like this as below:
From The Salary Largest Number is:12000
From The Salary Largest Number is:12000
From The Salary Largest Number is:20000
You need to keep track of two separate variables, one for the highest salary, and the other for the second highest salary. When assigning the highest salary, the previous highest salary should be clobbering the second highest. When assigning a new second highest, only the previous second highest should be overwritten.
Integer highest = null;
Integer second = null;
while ((record = br.readLine()) != null) {
String[] words = record.split(",");
arlist.add(Arrays.asList(words));
int salary = Integer.parseInt(words[3]);
if (highest == null) {
highest = salary;
second = salary;
}
else if (salary > highest) {
second = highest;
highest = salary;
}
else if (salary > second) {
second = salary;
}
}
System.out.println("highest salary: " + highest);
System.out.println("second highest salary: " + second);
System.out.println("From The Salary Largest Number is:" + maxSal); Keep this outside while loop.

Why is my Index was out of range in MVC 4?

I've tried to create some list with increase number after some calculation each month for a year.
For example, in month 1 number = 1, month 2 number = 3, month 3 number = 5.
The calculation is like this number[i] = i + number[i - 1]. Which i is the month.
I want to show all the list like this
Month[1] = 1,
Month[2] = 3,
Month[3] = 5,
Month[4] = 7,
Month[5] = 9,
Month[6] = 11,
Month[7] = 13,
...
Month[12] = 23
Here's my Controller
for (i = 1; i <= 12; i++)
{
List<int> number = new List<int>();
if (i <= 12)
{
number[i] = a(i, number[i - 1]);
}
else
{
//something else
}
}
Here's my a function
public int a(int month, int number)
{
try
{
a = month + number;
}
catch (Exception ex)
{
throw ex;
}
return a;
}
But, when executed i'm getting this error
Index was out of range. Must be non-negative and less than the size of the collection.
I've change the controller into this
for (i = 0; i <= 12; i++)
{
//...
}
But have the same error. Can someone help me? Why I'm getting this error?
Try this
Fiddle demo
int[] number = new int[13];
for (int i = 1; i <= 12; i++)
{
if (i <= 12)
{
number[i] = a(i, number[i - 1]);
}
else
{
//something else
}
}
int j = 1;
List<int> item = new List<int>();
foreach (var a in number)
{
if (j <= 12)
{
item.Add(a);
}
else
{
break;
}
j++;
}
public int a(int month, int number)
{
int a = 0;
try
{
a = month + number;
}
catch (Exception ex)
{
throw ex;
}
return a;
}

Encountering problems with JSFiddle code

I have recently written a piece of code that checks temperature over a period of time for some course work. i wrote the program using different bits of code but cannot find the issue to some of the problems i am encountering.
I was just wondering if anyone can see any obvious errors that i am missing and could help out with some information int he right direction.
Link to the JSFiddle
//Counting the average temperatures in a day
//needs debugged!!!
var temperatures = [];
var total = 0;
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
function getNumDays() {
number = prompt("How many days in the study? Enter 1 - 10");
while ((number < 1) || (number > 10) ||( isNaN(number) === true)) {
alert ("Invalid input!");
number = prompt ("Enter again, 1 - 10 >> ");}
return number;
}
function getTemps(numDays) {
total = 0;
for (i = 0; i < numDays; i++) {
next = prompt ("Enter the temperature for day " + (i+1));
next = parseint(next);
while (isNaN(next)===true) {
next = 0;
next = prompt ("Error in input! Try again >>");
next = parseInt(next);}
temperatures.push(next);
}
total = total + next;
return temperatures;
}
function calcAverage(total, numDays) {
average = total / numDays;
return average;
}
function showStatistics(city, average, numdays) {
alert ("The average daily temperature for "+ city + " is " + average.toFixed(2) + " measured over " + numDays + " days." );
}
//main program
city = getCity();
numDays = getNumDays();
temperatures = getTemps(numDays);
Average = calcAverage(total, numDays);
showStatistics(city, average, numDays);
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
//main program
city = getCity();
Looks like you're missing a return statement.
Additionally, the line total = total + next; seems to be out of place: I imagine the total to be the total of the temperatures, not 0 + temperatureOfLastDay.

how to auto test a C program use files?

I have a C program and I need to test it using a file containing some test data and output the results automatically. How can I do this? Should I modify the program or should I write another program using C or something else like Python? Which is easier?
Here is my C program:
int main()
{
double wh,sh,salary;
int num = 0;
int valid = 1;
printf("Please input two non-negative numbers as a employee's working hours in one week and salary per hour.\n");
printf("Notice that the salary per hour can't be greater than 1000\nAlso you can't input more than 10 sets of data\n");
printf("Input them like this:\n20,34\nthen enter the Enter key\n");
while(scanf("%lf,%lf",&wh,&sh) == 2 && num < 10)
{
if(sh <0 || wh <0)
{
printf("Salary per hour or salary per hour can't be negative!\n");
}
else if(wh > 168)
{
printf("Working hours in one week can't be more than 168!\n");
}
else if(sh > 1000)
{
printf("Salary can't be greater than 1000!\n");
}
else
{
num ++;
if(wh <= 40)
{
if(sh <= 1000)
{
salary = wh * sh;
}
else if(sh > 1000 )
{
salary = wh * 1000;
}
}
else if(wh > 40 && wh <= 50)
{
if(sh*1.5 < 1000)
{
salary = 40*sh + (wh-40)*1.5*sh;
}
else if(sh*1.5 > 1000 && sh < 1000)
{
salary = 40*sh + (wh-40)*1000;
}
else if(sh > 1000)
{
salary = wh * 1000;
}
}
else if(wh > 50)
{
if(sh*3 < 1000)
{
salary = 40*sh + 10*1.5*sh + (wh-50)*3*sh;
}
else if(sh*1.5 < 1000 && sh*3 >=1000)
{
salary = 40*sh + 10*1.5*sh + (wh-50)*1000;
}
else if(sh*1.5 >= 1000 && sh <= 1000)
{
salary = 40*sh + (wh-40)*1000;
}
else if(sh > 1000)
{
salary = wh*1000;
}
}
printf("The total working hours is %lf, and salary per hour is %lf, salary is %lf\n",wh,sh,salary);
}
}
return 0;
}
For this moment please ignore these literals floating around.
I suppose the file containing test data is like this:
1,2
34,34
67,43
...
I really have no idea how this test automation works,please help!
in cmd prompt
enter
c:\>myprog.exe < input.txt > output.txt
put all ur testcases in input.txt
ur output will print in the output.txt file