Need assignment idea - java

I need help with my assignment.I need to write class program that tranlates grade into grade point. If the grade have + like A+ it will increase the grade point by 0.3 and - will decrease by 0.3.
private static final double GradePoint = 0;
private static Scanner input;
public static void main(String [] args)
{
String grade ;
double GradePoint = 0;
System.out.print("Please enter your grade: ");
input = new Scanner(System.in);
grade = input.nextLine();
switch(grade)
{
case "A":
case "a": GradePoint = 4; break;
case "B":
case "b": GradePoint = 3; break;
case "C":
case "c": GradePoint = 2; break;
case "D":
case "d": GradePoint = 1; break;
case "F":
case "f": GradePoint = 0; break;
}
System.out.print("Your grade is: "+GradePoint);
}
public double getGradePoint(String grade)
{
return GradePoint;
}
What i dont understand is about how to use the method to calculate.I'm still beginner.

I have to use CLASS and method*public double getGradePoint(String grade)* to
return the grade point of grade entered.
You need to shift your entire code from main() to getGradePoint(String grade);
also your switch case switch(grade) will not work for values like "A+" as there are no such case that matches the string "A+"

I was bored and had nothing better to do so here :)
public class GradeCalculator
{
public static void main(String[] args)
{
System.out.print("Please enter your grade: ");
Scanner input = new Scanner(System.in);
String grade = input.nextLine().trim();
GradeCalculator calculator = new GradeCalculator();
double gradePoint = calculator.getGradePoint(grade);
System.out.print("Your grade is: " + gradePoint);
}
private double getGradePoint(String grade)
{
int score = getGradeScore(grade.charAt(0));
double modifier = 0;
if (grade.length() > 1)
{
modifier = getModifierValue(grade.charAt(1));
}
return score + modifier;
}
private int getGradeScore(char grade)
{
int score = 0;
switch (grade)
{
case 'A':
case 'a':
score = 4;
break;
case 'B':
case 'b':
score = 3;
break;
case 'C':
case 'c':
score = 2;
break;
case 'D':
case 'd':
score = 1;
break;
case 'F':
case 'f':
score = 0;
break;
}
return score;
}
private double getModifierValue(char modifier)
{
double value = 0;
switch (modifier)
{
case '+':
value = 0.3;
break;
case '-':
value = -0.3;
break;
}
return value;
}
}

Related

Ways to reverse print a string?

I'm trying to code a program that would convert an int to binaries. So far I have it print out the remainders, but it has to be printed in reverse for it to be a proper binary. I am not allowed to use any methods.
Here's my code.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int j = i; //backup
int k = 0; //remainder
while (j != 0) {
k = j % 2;
j /= 2;
String reversedStr = "";
switch (k) {
case 0:
reversedStr += "0";
break;
case 1:
reversedStr += "1";
break;
case 2:
reversedStr += "2";
break;
case 3:
reversedStr += "3";
break;
case 4:
reversedStr += "4";
break;
case 5:
reversedStr += "5";
break;
case 6:
reversedStr += "6";
break;
case 7:
reversedStr += "7";
break;
case 8:
reversedStr += "8";
break;
case 9:
reversedStr += "9";
break;
case 10:
reversedStr += "A";
break;
case 11:
reversedStr += "B";
break;
case 12:
reversedStr += "C";
break;
case 13:
reversedStr += "D";
break;
case 14:
reversedStr += "E";
break;
case 15:
reversedStr += "F";
break;
}
for (int l = reversedStr.length() - 1; l >= 0; l--) {
reversedStr.charAt(i);
System.out.print(reversedStr.charAt(l));
}
}
System.out.println("done");
}
}
this code gives me
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 178
at java.lang.String.charAt(String.java:658)
at radixHandmade.Handmade.main(Handmade.java:73)
The for loop at the bottom is the reversing part, but im not sure how to use charAt to complete the code. To be honest im very confused.
Any help would be greatly appreciated.
This statement causes the problem:
reversedStr.charAt(i);
Aside from causing an exception (because you're trying to get the (originally entered value)-th character in the string, which is by outside the bounds of the string, unless that value happens to be 0), it has no other side effect.
Just remove this line.
import java.util.Scanner;
public class Reverse {
public static void main(String[] args){
userInput();
}
public static void userInput(){
Scanner console = new Scanner(System.in);
String reverseString = console.nextLine(); //This is the word to be written backwards.
int s = reverseString.length()-1; //length of the word.
for (int i = s ; i>=0; i--){
System.out.print(reverseString.charAt(i));
}
}
}
Hope this helps.
Good luck.
removing the line reversedStr.charAt(i) will resolve the issue, but the program will still not convert to binary correctly. Your string still needs to be reversed. Might I suggest using the StringBuilder class:
import java.util.Scanner;
import java.lang.StringBuilder;
public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder out = new StringBuilder();
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int r = 0;
int b = 2; // now you can just change this for what base (binary, hex, ...)
while (i != 0) {
r = i % b;
i /= b;
switch (r) {
case 0:
out.append("0");
break;
case 1:
out.append("1");
break;
}
}
System.out.println(out.reverse());
}
}
Your code would work fine if you move the reversing for loop out of the while loop:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String revStr = "";
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int r = 0;
int b = 2; // now you can just change this for what base (binary, hex, ...)
while (i != 0) {
r = i % b;
i /= b;
switch (r) {
case 0:
revStr += "0";
break;
case 1:
revStr += "1";
break;
// ... for hex
}
}
for (int j = revStr.length() - 1; j >= 0; j--) {
System.out.print(revStr.charAt(j));
}
System.out.print("\n");
}
But what I really don't understand is why you even have to reverse the string if you can just do:
reversedStr = "0" + reversedStr; // of course it will no longer be reversed
// ... for all the cases
Even better if you stick with just binary, you could skip the whole switch and do
public static void main(String[] args) {
String str = "";
System.out.println("Enter A Postive Number:");
for(int i = new Scanner(System.in).nextInt(); i != 0; i /= 2)
str = String.valueOf(i % 2) + str;
System.out.println(str);
}

Java error: method in class cannot be applied to given types

I am just trying to call the methods to the main for each switch when it happens, but i just get the error message everytime i try to call any methods, not trying to return anything. ex. if the user enters a or A i want to call the add method to main
public static void main(String[] args)
{
char character;
Scanner keyboard = new Scanner(System.in);
while (character != 'E' || character != 'e')
{
System.out.println(" A:Addition \n S:Subtraction \n M:Multiplication \n D:Division \n R:Modulus \n E:exit");
switch (character)
{
case 'a':
case 'A':
System.out.println("your choice A");
add();
break;
case 's':
case 'S':
System.out.println("your choice S");
subtraction();
break;
case 'm':
case 'M':
System.out.println("your choice M");
multiplication();
break;
case 'd':
case 'D':
System.out.print("your choice D");
division();
break;
case 'r':
case 'R':
System.out.println("your choice R");
modulus();
break;
default:
System.out.println("Error: please enter a valid letter");
break;
}
}
}
public static void add(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a + b;
System.out.println(a + "plus" + b + "is" + total );
}
public static void subtraction(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a-b;
System.out.println(a + "minus" + b + "is " + total);
}
public static void multiplication(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a*b;
System.out.println(a + "times" + b + "is " + total);
}
public static void division(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a/b;
System.out.println(a + "divided" + b + "is " + total);
}
public static void modulus(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total= a%b;
System.out.println(a + "modulus" + b + "is " + total);
System.out.println("The program is terminating");
}
}
you're calling the method but you didn't include an argument
take a look at this.
public static void add(Scanner keyboard)
you have an argument, so you must include an argument when calling this method
so
you must call the method like this.
add(keyboard);
You have defined the method which takes Scanner as argument but you are calling the methods with no args.
All the method you are using are supposed to receive a Scanner object while you pass no argument.
For example you call add(); while it signature is
public static void add(Scanner keyboard)
Which is why you get the error.
Instead, use add(keyboard) and repeat the same for substraction, multiplication, division and modulus methods.
So that your switch would now look like
switch (character) {
case 'a':
case 'A':
System.out.println("your choice A");
add(keyboard);
break;
case 's':
case 'S':
System.out.println("your choice S");
subtraction(keyboard);
break;
case 'm':
case 'M':
System.out.println("your choice M");
multiplication(keyboard);
break;
case 'd':
case 'D':
System.out.print("your choice D");
division(keyboard);
break;
case 'r':
case 'R':
System.out.println("your choice R");
modulus(keyboard);
break;
default:
System.out.println("Error: please enter a valid letter");
break;
}
You are missing the arguments in the method call.
case 'a':
case 'A':
System.out.println("your choice A");
add(keyboard); // Add arguments.
break;

could use some help on this decode(char c) method

The program is to write a calss PhoneNumber.java
I understand that I am supposed to test if the string is a digit or a letter and then if it is a letter its supposed to be decoded by decode(char c);
However, I dont think char c should be in between the ( ) If any one has suggestions thatd be great thanks!! The toString is left unreturned intentionally because i have not gotten that far in the program yet. Also, have to keep it in the case 'A' format Thanks
public class PhoneNumber {
private int areacode;
private int number;
private int ext;
PhoneNumber() {
areacode = 0;
number = 0;
ext = 0;
}
PhoneNumber(int newnumber) {
areacode = 216;
number = newnumber;
ext = 0;
}
PhoneNumber(int newarea, int newnumber, int newext) {
areacode = newarea;
number = newnumber;
ext = newext;
}
PhoneNumber(String newnumber) {
String areacode = str[0];
String number = str[1];
String[] str = newnumber.split("-");
String[] number = newnumber;
boolean b1, b2;
int i = 0;
int place = 0;
for (int x: newnumber){
newnumber.charAt[i] = place;
b1 = Character.isDigit(place);
if (b1 == true){
number = place;
i++;
} else {
b2 = Character.isLetter(place);
} if (b2 == true) {
number = decode(place);
i++;
} else {
System.out.print("invalid phone number!");
}
}
System.out.print(areacode.concat(number));
return newnumber;
}
private String decode(place) {
switch (c) {
case 'A': case 'B': case 'C': return "2";
case 'D': case 'E': case 'F': return "3";
case 'G': case 'H': case 'I': return "4";
case 'J': case 'K': case 'L': return "5";
case 'M': case 'N': case 'O': return "6";
case 'P': case 'Q': case 'R': case 'S': return "7";
case 'T': case 'U': case 'V': return "8";
case 'W': case 'X': case 'Y': case 'z': return "9";
default: return "";
}
}
public boolean equals(PhoneNumber pn) {
}
public String toString() {
}
}
G:\CIS260\Assignments>javac PhoneNumber.java
PhoneNumber.java:53: error: <identifier> expected
private String decode(place) {
^
1 error
In the constructor, you need to declare the array before you put things in it. You also can't say String[] number = newnumber because number is a String[] and newnumber is a String. equals() and toString() need to return something. And, to answer your question, just say
private String decode(char c){

Bracket expected.... not sure where [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I'm doing an assignment on modular programing and here Im getting a bracket expected error. Here is the code:
import java.util.*;
public class stlab09
{
public static void main (String args[])
{
System.out.println("\nLAB09 90 POINT VERSION\n\n");
enterData();
computeGPA();
displayData();
}
static String lGrade1;
static String lGrade2;
static String lGrade3;
static String lGrade4;
static int cHours1;
static int cHours2;
static int cHours3;
static int cHours4;
static String dummy;
public static double gpa;
public static void enterData()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter course 1 Grade ===>> ");
lGrade1 = in.nextLine();
System.out.print("enter course 1 Hours ===>> ");
cHours1 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 2 Grade ===>> ");
lGrade2 = in.nextLine();
System.out.print("enter course 2 Hours ===>> ");
cHours2 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 3 Grade ===>> ");
lGrade3 = in.nextLine();
System.out.print("enter course 3 Hours ===>> ");
cHours3 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 4 Grade ===>> ");
lGrade4 = in.nextLine();
System.out.print("enter course 4 Hours ===>> ");
cHours4 = in.nextInt(); dummy = in.nextLine();
}
public static void computeGPA()
{
Grades.gradeValue();
Grades.courseValue();
Grades.getGPA();
}
public static void displayData()
{
System.out.println();
System.out.println("Course1 Grade: " + lGrade1 + " Course1 Credit Hours: " + cHours1);
System.out.println("Course2 Grade: " + lGrade2 + " Course2 Credit Hours: " + cHours2);
System.out.println("Course3 Grade: " + lGrade3 + " Course3 Credit Hours: " + cHours3);
System.out.println("Course4 Grade: " + lGrade4 + " Course4 Credit Hours: " + cHours4);
System.out.println();
System.out.println("Current GPA: " + gpa);
}
}
public class Grades() ***<<<<<<<<<<<<<<<<<< bracket expected here***
{
public static void gradeValue()
{
int value = 0;
char lg1 = lGrade1.charAt(0);
switch(lg1)
{
case 'A': value = 4; break;
case 'B': value = 3; break;
case 'C': value = 2; break;
case 'D': value = 1; break;
case 'F': value = 0; break;
}
int gVal1 = value;
char lg2 = lGrade2.charAt(0);
switch(lg2)
{
case 'A': value = 4; break;
case 'B': value = 3; break;
case 'C': value = 2; break;
case 'D': value = 1; break;
case 'F': value = 0; break;
}
int gVal2 = value;
char lg3 = lGrade3.charAt(0);
switch(lg3)
{
case 'A': value = 4; break;
case 'B': value = 3; break;
case 'C': value = 2; break;
case 'D': value = 1; break;
case 'F': value = 0; break;
}
int gVal3 = value;
char lg4 = lGrade4.charAt(0);
switch(lg4)
{
case 'A': value = 4; break;
case 'B': value = 3; break;
case 'C': value = 2; break;
case 'D': value = 1; break;
case 'F': value = 0; break;
}
int gVal4 = value;
}
public static void courseValue()
{
int cVal1 = gVal1 * cHours1;
int cVal2 = gVal2 * cHours2;
int cVal3 = gVal3 * cHours3;
int cVal4 = gVal4 * cHours4;
}
public static void getGPA()
{
double totalValue = cVal1 + cVal2 + cVal3 + cVal4;
double totalHours = cHours1 + cHours2 + cHours3 + cHours4;
double gpa = totalValue / totalHours;
}
}
So yeah I need some help figuring this out because I'm kinda going crazy about it. The expected program is supposed to use keyboard input of letter grades and course hours to compute GPA and grades. The assignment is to get that outcome but the main method must stay exactly as is, and almost every method was provided to me and i just had to organize them.
You have declared the inner class Grades as if it's a method (you added () onto the end of it), look at how the class stlab09 is declared, there aren't any ().

Now am getting incompatible error when using the if else statements

trying to run the program below but am getting an error when using the switch method
import java.util.Scanner;
/**
*
* #author kern
public class loans {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//variabled decleared
double rate, payment,principal,interest,n;
int length;
String period;
//input
System.out.print("Enter the amount of money borrowed: $");
principal = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
interest = input.nextDouble();
System.out.print("Enter the payment period :");
period = input.next();
System.out.print("Enter Loan Length:");
length = input.nextInt();
//process
rate=interest/100;
payment= principal*(rate*Math.pow((1+rate),n)/ Math.pow ((1+rate),n));
if (period==annually) {
n=1*length;
System.out.prtintf(Your monthly sum is %f:,payment);{
if (period==semiannuall) {
n=2*length;
System.out.prtintf(Your monthly sum is %f:,payment);{
if (period== quarterly) {
n=4*length;
System.out.prtintf(Your quarterly sum is %f:,payment);{
if (period==monthly) {
n=12*length;
System.out.prtintf(Your monthly sum is %f:,payment);{
}
}
String as case value are supported from java 7
See
Switch case
You need to use it like
if("annually".equals(period)){
}
Per Jigar Joshi link you can do strings now:
Using Strings in switch Statements
In Java SE 7 and later, you can use a String object in the switch statement's expression. The following code example, StringSwitchDemo, displays the number of the month based on the value of the String named month:
public class StringSwitchDemo {
public static int getMonthNumber(String month) {
int monthNumber = 0;
if (month == null) {
return monthNumber;
}
switch (month.toLowerCase()) {
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
case "april":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "june":
monthNumber = 6;
break;
case "july":
monthNumber = 7;
break;
case "august":
monthNumber = 8;
break;
case "september":
monthNumber = 9;
break;
case "october":
monthNumber = 10;
break;
case "november":
monthNumber = 11;
break;
case "december":
monthNumber = 12;
break;
default:
monthNumber = 0;
break;
}
return monthNumber;
}
public static void main(String[] args) {
String month = "August";
int returnedMonthNumber =
StringSwitchDemo.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("Invalid month");
} else {
System.out.println(returnedMonthNumber);
}
}
}

Categories

Resources