Java Homework Problems:
4.
• Examine AddImport.java.
– Perform the following:
– Replace the fully qualified name to access the Jlabel
component with an import statement.
– To import classes from the util package, replace multiple
import statements with a single import statement.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Calendar;
import java.util.Date;
public class AddImport {
public static void main(String args[]) {
javax.swing.JLabel label = new javax.swing.JLabel("hello");
}
}
===========================================================
5.
Exercise 1
• Examine ShoppingCart.java.
– Perform the following:
– Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx.
– Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName.
public class ShoppingCart {
public static void main (String[] args){
String custName = "Steve Smith";
String firstName;
int spaceIdx;
// Get the index of the space character (" ") in custName.
// Use the substring method parse out the first name and print it.
}
}
Exercise 2
Examine NameMaker.java.
• Perform the following:
– Declare String variables: firstName, middleName, lastName, and fullName
– Prompt users to enter their first, middle, and last names and read the names from the keyboard.
– Set and display the fullName as firstName+a blank char+middleName+a blank char+lastName.
public class NameMaker {
public static void main(String args[])
{
}
}
Exercise 2
• Which do you think is preferable for this scenario?
• That is, the string concatenation operator or the concat() method?
AddImport.java
//To import classes from the util package, replace
multiple public class AddImport { public static void main(String args[]) { } } |
Output
5. Exercise 1
ShoppingCart.java
public class ShoppingCart { public static void main (String[] args){ String custName = "Steve Smith"; String firstName; int spaceIdx; // Get the index of the space character (" ") in custName. |
Output
Exercise 2
NameMaker.java
/ //To import classes from the util package public static void main(String args[]){ System.out.println("Full Name is
: "+ fullName + )" using + operator"); |
Output
EXERCISE 2
As while using the concatenate + operator you can concatenate A string with any data type.
like you can concat numbers with strings
example: String numberOfBats = 10 + " Bats";
OUTPUT here will be: 10 Bats
But a concatenate method could only be able to concatenate strings (i.e you can't concat strings with numbers etc.)
As in this question, All three user inputs are Strings. you can easily use either concat using append or use + operator without using the concat method.
Here I will prefer the + operator method.
Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified...
I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel { private static final int...
read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....
8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private, and add appropriate accessor methods to the class named getFirstName, getMiddleInitial, and getLastName. code: class Name { private String firstName; private String middleNames; private String lastName; //Constructor method public Name(String firstName, String middleNames, String lastName) { this.firstName = firstName; this.middleNames = middleNames; this.lastName = lastName; } //Accessor for firstName public String getFirstName() { return firstName; } //Accessor for middleNames public String getMiddlesNames()...
Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya import java.util.Scanner; public class SpaceReplace { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); String firstName; String lastName; //answer goes here// } }
JAVA Can you make this return the first letter of first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 { public static void main(String[] args) { //scanner input from user Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...
In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); } public static String printBackwards(String one)...
Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...
Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...
FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...
Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13 Assignment 13 Preparation This assignment will focus on the use of object methods during event handling. Assignment 13 Assignment 13 Submission Follow the directions below to submit Assignment 13: This assignment will be a modification of the Assignment 12 program (see EncryptionApplication11.java below). Replace the use of String concatenation operations in the methods with StringBuilder or StringBuffer objects and their methods. EncryptTextMethods.java ====================== package...