PDA

View Full Version : Elements second Java assignment!



Element17
09-08-2013, 10:46 PM
First off thanks for the help last time guys! I will post my assignment later tonight once I finish it! But I do have one question about something my teach asked...


"Two variables*, num and cost have been declared* and given* values*: num is an integer* and cost is a double*. Write a single statement* that outputs* num and cost to standard output*. Print both values* (num first, then cost), separated by a space on a single line that is terminated with a newline character*. Do not output* any thing else."


No idea what he is really asking for there....





import java.util.Scanner;

/*Andrew Varnick
9/7/2013
CSCI 1015
Programming Assignment 2
The purpose of this assignment/program is to calculate the number of cents in a specific number of dimes and nickels
*/

public class VarnickPass2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String name, input1, input2;
int nickle, dime, total;


//Get the user's name
name =
keyboard.nextLine();

//Get the number of nickles and dimes

input = keyboard.nextLine();
input2 = keyboard.nextLine();


//Convert the input to Int
nickle = Integer.parseInt(input);
dime = Integer.parseInt(input2);

//Gets total
System.out.println("Total is:" + ((nickle * 5) + (dime * 10)));



System.exit(0);
}
}


Assignment #2 (http://villavu.com/forum/usertag.php?do=list&action=hash&hash=2) !!!!!

Purpose

To learn how to create the main java method, explore declaring a variables, using
arithmetic operators, assignment statements, reading from the keyboard using the
Scanner class and outputting results using
System.out class, and adding comments.


Exercise

Write a program that will calculate the number of cents in a specific number of dimes
and nickels. The program will perform the following tasks:
•Ask the user to enter the number of dimes.
•Ask the user to enter the number of nickels.
•Calculate the total number of cents.
•Display to the monitor the number of cents.

Kasi
09-08-2013, 11:09 PM
I think you should attempt the exercise first. but



System.out.println(num + " " + cost);

riwu
09-09-2013, 12:15 AM
public static void main(String[] args) throws IOException {
BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of dimes: ");
String dimes = Br.readLine();
System.out.print("Enter the number of nickels: ");
String nickels = Br.readLine();
int d = 0;
int n = 0;
try{
d = Integer.parseInt(dimes);
n = Integer.parseInt(nickels);
}catch(NumberFormatException nfe){
System.err.println("Enter only numbers!");
}
System.out.println("Total value: " + (d * 10 + n * 5) + " cents");
}

Brandon
09-09-2013, 12:20 AM
..Code..



This is incorrect.

Purpose of his exercise:


Reading from the keyboard using the java.util.Scanner class and outputting results..


Thus you shouldn't be using a buffered-reader and a try catch for evaluating invalid input.

Also, you might want to give OP a chance to do the assignment rather than him not an showing any attempt at doing it. He will never learn this way. Other than that, nice code.

riwu
09-09-2013, 12:33 AM
Well in the beginning it's best to learn by examples...(unless OP requested to give only hints and not solutions)
If you have to use the Scanner class:


public static void main(String[] args) {
Scanner Sc = new Scanner(System.in);
int d = 0;
int n = 0;
System.out.print("Enter the number of dimes: ");
try {
d = Sc.nextInt();
}catch (java.util.InputMismatchException ime) {
System.err.println("Enter only numbers!");
System.exit(0);
}
System.out.print("Enter the number of nickels: ");
try {
n = Sc.nextInt();
}catch (java.util.InputMismatchException ime) {
System.err.println("Enter only numbers!");
System.exit(0);
}
System.out.println("Total value: " + (d * 10 + n * 5) + " cents");
}

Element17
09-10-2013, 02:23 AM
import java.util.Scanner;
import javax.swing.JOptionPane;

/*Andrew Varnick
9/7/2013
CSCI 1015
Programming Assignment 2
The purpose of this assignment/program is to calculate the number of cents in a specific number of dimes and nickels
*/

public class VarnickPass2
{
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
String name;
String input, input2;
int nickle, dime, total;


//Get the user's name
name =
JOptionPane.showInputDialog("Enter your name.");

//Get the number of nickles and dimes

input = JOptionPane.showInputDialog("How many nickles do you have?");
input2 = JOptionPane.showInputDialog("How many dimes do you have");


//Convert the input to Int
nickle = Integer.parseInt(input);
dime = Integer.parseInt(input2);

//Gets total
System.out.println("Total is" + nickle * 5 + dime * 10);



System.exit(0);
}
}

riwu
09-10-2013, 10:29 AM
import java.util.Scanner;
import javax.swing.JOptionPane;

/*Andrew Varnick
9/7/2013
CSCI 1015
Programming Assignment 2
The purpose of this assignment/program is to calculate the number of cents in a specific number of dimes and nickels
*/

public class VarnickPass2
{
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
String name;
String input, input2;
int nickle, dime, total;


//Get the user's name
name =
JOptionPane.showInputDialog("Enter your name.");

//Get the number of nickles and dimes

input = JOptionPane.showInputDialog("How many nickles do you have?");
input2 = JOptionPane.showInputDialog("How many dimes do you have");


//Convert the input to Int
nickle = Integer.parseInt(input);
dime = Integer.parseInt(input2);

//Gets total
System.out.println("Total is" + nickle * 5 + dime * 10);



System.exit(0);
}
}

hmm you instantiated the Scanner class, but ended up using swing GUI for input :S
Also without putting parenthesis for nickle * 5 + dime * 10, it will concatenate the string rather than the integer sum.

[XoL]
09-10-2013, 01:42 PM
I know when i was in Java 1 my prof hated when people did any sort of math in the system.out. line, not sure if yours is the same.

Element17
09-10-2013, 07:45 PM
;1264815']I know when i was in Java 1 my prof hated when people did any sort of math in the system.out. line, not sure if yours is the same.

He hasn't said anything about it, but how else could I print the results?


Also to @riwu should I take out the GUI? and should the last line to calculate be something like this


System.out.println("Total is" + ((nickle * 5) + (dime * 10)));

riwu
09-10-2013, 11:06 PM
He hasn't said anything about it, but how else could I print the results?


Also to @riwu should I take out the GUI? and should the last line to calculate be something like this


System.out.println("Total is" + ((nickle * 5) + (dime * 10)));

Xol's prof wants the math to be done in advance, like
Int value = nickle * 5 + dime * 10;

You should add parenthesis to the whole math block as seen in my prev 2 code:


System.out.println("Total value: " + (d * 10 + n * 5) + " cents");