Phân chia mảng số nguyên thành 2 phần + chắc + lẻ
Câu 1: Nhập mảng số nguyên gồm N phần tử (khai báo theo cách int[] t = new int[N]). Thực hiện chuyển các số chẵn sang bên trái và số lẻ sang bên phải theo thứ tự tăng dần
Ví du: mảng nhập vào [1, 9, 2, 7, 10, 4, 5, 6]
Kết quả mảng [2, 4, 6, 10, 1, 5, 7, 9]
Câu 2: Tạo đối tượng sinh viên gồm các thuộc tính sau >> Tên, tuổi, địa chỉ, email, số điện thoại.
Trong class Main tạo 2 đối tượng sinh viên stdA và stdB. Kiểm tra xem trong các thuộc tính của 2 sinh viên => xuất hiện chuỗi searching (chuỗi này được nhập từ bàn phím).
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Thành Lâm [T1907A]](https://www.gravatar.com/avatar/fb1b94f4caad069ee6e3f42ea2221b49.jpg?s=80&d=mm&r=g)
Thành Lâm
2020-05-11 10:00:41
Câu 2 đề 1: em thật sự xin lỗi vì nộp bài muộn thế này.
![Đỗ tuấn anh [T1907A]](https://www.gravatar.com/avatar/98f1e29922dc48dd5d41b8f6cbce24e0.jpg?s=80&d=mm&r=g)
Đỗ tuấn anh
2020-05-11 08:27:34
/*
* 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.
*/
package Thi;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Main {
public static void main(String[] args){
List<Student> studentlist = new ArrayList<>();
int choise, n;
Scanner scan = new Scanner(System.in);
do {
menu();
choise=Integer.parseInt(scan.nextLine());
switch(choise){
case 1:
System.out.println("nhap so sinh vien");
n=Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
Student Student = new Student();
Student.input();
Student.add(Student);
}
break;
case 2:
for (Student std : studentlist) {
std.display();
}
break;
case 3:
System.out.println("nhap email cần tìm kiếm!!!!");
String email=scan.nextLine();
for (int i = 0; i <Student.size(); i++) {
if(studentlist.get(i).getEmail().equalsIgnoreCase(email)){
studentlist.get(i).display();
}
}
break;
}
} while (choise!=3);
}
static void menu(){
System.out.println("1.nhập thông tin sinh viên : ");
System.out.println("2.hiển thị!!");
System.out.println("3.tìm kiếm?");
}
}
main
![Phan Bạch Tùng Dương [T1907A]](https://www.gravatar.com/avatar/e74e3ec62fe3a191929e12eecbe01edf.jpg?s=80&d=mm&r=g)
Phan Bạch Tùng Dương
2020-05-11 08:24:54
Đề 2: câu 2
/*
* 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.
*/
package StudentElement;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Main {
static List<SinhVien> studentList = new ArrayList();
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int choose;
System.out.println("1. Them sinh vien");
System.out.println("2. Show sv");
System.out.println("Nhap lua chon cua ban");
do {
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
addStudent();
break;
case 2:
showStudent();
break;
default:
System.out.println("Exit!!");
break;
}
} while (choose != 3);
}
static void addStudent() {
System.out.println("Them sinh vien:");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
SinhVien std = new SinhVien();
std.input();
studentList.add(std);
}
}
static void showStudent() {
System.out.println("Show all students: ");
studentList.forEach((sv) -> {
sv.display();
});
}
}
/*
* 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.
*/
package StudentElement;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class SinhVien {
String ten,diachi,email;
int sodienthoai,tuoi;
public SinhVien() {
}
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public String getDiachi() {
return diachi;
}
public void setDiachi(String diachi) {
this.diachi = diachi;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getSodienthoai() {
return sodienthoai;
}
public void setSodienthoai(int sodienthoai) {
this.sodienthoai = sodienthoai;
}
public int getTuoi() {
return tuoi;
}
public void setTuoi(int tuoi) {
this.tuoi = tuoi;
}
@Override
public String toString() {
return "sinhvien{" + "ten=" + ten + ", diachi=" + diachi + ", email=" + email + ", sodienthoai=" + sodienthoai + ", tuoi=" + tuoi + '}';
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Ho va ten: ");
ten = scan.nextLine();
System.out.println("Tuoi: ");
tuoi = Integer.parseInt(scan.nextLine());
System.out.println("Dia chi: ");
diachi = scan.nextLine();
System.out.println("Email: ");
email = scan.nextLine();
System.out.println("So dien thoai : ");
sodienthoai = Integer.parseInt(scan.nextLine());
}
void display() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
![Đường Thanh Bình [T1907A]](https://www.gravatar.com/avatar/c2ef7c316acb82467912bf5677b52a8b.jpg?s=80&d=mm&r=g)
Đường Thanh Bình
2020-05-11 08:23:17
đề 2
câu 2
/*
* 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.
*/
package Test1;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Main {
public static void main(String[] args){
List<Student> studentlist = new ArrayList<>();
int choise, n;
Scanner scan = new Scanner(System.in);
do {
menu();
choise=Integer.parseInt(scan.nextLine());
switch(choise){
case 1:
System.out.println("nhap so sinh vien");
n=Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
Student Student = new Student();
Student.input();
Student.add(Student);
}
break;
case 2:
for (Student std : studentlist) {
std.display();
}
break;
case 3:
System.out.println("nhap email cần tìm kiếm");
String email=scan.nextLine();
for (int i = 0; i <Student.size(); i++) {
if(studentlist.get(i).getEmail().equalsIgnoreCase(email)){
studentlist.get(i).display();
}
}
break;
}
} while (choise!=3);
}
static void menu(){
System.out.println("1.nhập thông tin sinh viên:");
System.out.println("2.hiển thị");
System.out.println("3.tìm kiếm");
}
}
/*
* 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.
*/
package Test1;
/**
*
* @author Administrator
*/
public class Student {
static int size() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
String fullname,email,address;
int age,phonenumber;
public Student() {
}
public Student(String fullname, String email, String address, int age, int phonenumber) {
this.fullname = fullname;
this.email = email;
this.address = address;
this.age = age;
this.phonenumber = phonenumber;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(int phonenumber) {
this.phonenumber = phonenumber;
}
@Override
public String toString() {
return "Student{" + "fullname=" + fullname + ", email=" + email + ", address=" + address + ", age=" + age + ", phonenumber=" + phonenumber + '}';
}
void input() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void add(Student Student) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void display() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
![Phí Văn Long [T1907A]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
Phí Văn Long
2020-05-11 08:18:30
/*
* 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.
*/
package LoginJFrame;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Bt1 {
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Nhập số phần tử của mảng: ");
int n = scanner.nextInt();
// khởi tạo arr
int [] t = new int [n];
System.out.print("Nhập các phần tử của mảng: \n");
for (int i = 0; i < n; i++) {
System.out.printf("a[%d] = ", i);
t[i] = scanner.nextInt();
}
// sắp xếp dãy số theo thứ tự tăng dần
sortASC(t);
System.out.println("Dãy số được sắp xếp: ");
show(t);
}
public static void sortASC(int [] arr) {
int temp = arr[0];
for (int i = 0 ; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
public static void show(int [] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
bài 2 :
/*
* 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.
*/
package LoginJFrame;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class sinhvien {
String ten,diachi,email;
int sodienthoai,tuoi;
public sinhvien() {
}
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public String getDiachi() {
return diachi;
}
public void setDiachi(String diachi) {
this.diachi = diachi;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getSodienthoai() {
return sodienthoai;
}
public void setSodienthoai(int sodienthoai) {
this.sodienthoai = sodienthoai;
}
public int getTuoi() {
return tuoi;
}
public void setTuoi(int tuoi) {
this.tuoi = tuoi;
}
@Override
public String toString() {
return "sinhvien{" + "ten=" + ten + ", diachi=" + diachi + ", email=" + email + ", sodienthoai=" + sodienthoai + ", tuoi=" + tuoi + '}';
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Ho va ten: ");
ten = scan.nextLine();
System.out.println("Tuoi: ");
tuoi = Integer.parseInt(scan.nextLine());
System.out.println("Dia chi: ");
diachi = scan.nextLine();
System.out.println("Email: ");
email = scan.nextLine();
System.out.println("So dien thoai : ");
sodienthoai = Integer.parseInt(scan.nextLine());
}
public void display() {
System.out.println("toString()");
}
}
/*
* 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.
*/
package LoginJFrame;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Main2 {
static List<sinhvien> studentList = new ArrayList();
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int choose;
System.out.println("1. Them sinh vien");
System.out.println("2. Show sv");
System.out.println("Nhap lua chon cua ban");
do {
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
addStudent();
break;
case 2:
showStudent();
break;
default:
System.out.println("Exit!!");
break;
}
} while (choose != 3);
}
static void addStudent() {
System.out.println("Them sinh vien:");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
sinhvien std = new sinhvien();
std.input();
studentList.add(std);
}
}
static void showStudent() {
System.out.println("Show all students: ");
for (sinhvien sv : studentList) {
sv.display();
}
}
}
![Trần Ngọc Hải [T1907A]](https://www.gravatar.com/avatar/9f7a27af52cbf0d52c32019e1354d60a.jpg?s=80&d=mm&r=g)
Trần Ngọc Hải
2020-05-11 08:18:20
Thêm class đề 2 bài 2 ( nãy em gửi thiếu 1 class )
/*
* 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.
*/
/**
*
* @author Admin
*/
public class Student {
String name , address , email , phonenumber;
int age;
public Student()
{
}
public Student(String name, String address, String email, String phonenumber, int age) {
this.name = name;
this.address = address;
this.email = email;
this.phonenumber = phonenumber;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" + "name=" + name + ", address=" + address + ", email=" + email + ", phonenumber=" + phonenumber + ", age=" + age + '}';
}
public void input()
{
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten :");
name = scan.nextLine();
System.out.println("Nhap address");
address = scan.nextLine();
System.out.println("Nhap email");
email = scan.nextLine();
System.out.println("Nhap phonenumber");
phonenumber = scan.nextLine();
System.out.println("Nhap age");
age = Integer.parseInt(scan.nextLine());
}
public void display()
{
System.out.println(toString());
}
}
![Minh Nghia [T1907A]](https://www.gravatar.com/avatar/ecca255d725eed36a872105205af1b8e.jpg?s=80&d=mm&r=g)
Minh Nghia
2020-05-11 08:18:18
/*
* 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.
*/
package Examination;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class sortchanle {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N ,c ,l ;
System.out.println("Nhap vao phan tu cua mang:");
N = Integer.parseInt(scan.nextLine());
int[] t = new int[N];
int[] chan = new int[N];
int[] le = new int[N];
for (int i = 0; i < N; i++) {
System.out.println("Nhap phan tu thu : " + i);
t[i] = scan.nextInt();
}
c = l = 0;
for (int i = 0; i < N; i++) {
if(t[i] %2 == 1){
chan[c] = t[i];
c ++;
}else{
le[i] = t[i];
l ++;
}
}
sort(chan);
sort(le);
System.out.println("Mang sau khi duoc sap xep :");
display(le);
display(chan);
}
public static void sort(int[] arr){
int sx = arr[0];
for (int i = 0; i < arr.length -1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if(arr[i] > arr[j]){
sx = arr[j];
arr[j] = arr[i];
arr[i] = sx;
}
}
}
}
public static void display(int[] arr){
for (int i = 0; i < arr.length; i++) {
if(arr[i] != 0){
System.out.print(arr[i] + ",");
}
}
}
}
![Trần Anh Quân [T1907A]](https://www.gravatar.com/avatar/7e3a8fe30cedd711f426fc59a9d48efc.jpg?s=80&d=mm&r=g)
Trần Anh Quân
2020-05-11 08:14:42
.
![Trần Ngọc Hải [T1907A]](https://www.gravatar.com/avatar/9f7a27af52cbf0d52c32019e1354d60a.jpg?s=80&d=mm&r=g)
Trần Ngọc Hải
2020-05-11 08:14:19
Đề 2 - bài 1 :
/*
* 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.
*/
/**
*
* @author Admin
*/
public static void main(String[] args) {
int N ;
Scanner scan = new Scanner(System.in);
N = Integer.parseInt(scan.nextLine());
int[] intArray = new int[N] ;
int k = 0 ;
int[] intArray01 = new int[N];
for ( int i = 0 ; i < N ; i++ )
{
intArray[i] = scan.nextInt();
}
for ( int i = 0 ; i < intArray.length ; i++)
{
if ( intArray[i]%2 == 1)
{
k++;
}
}
for ( int i = 0 ; i < N - 1 ; i++ )
{
for ( int j = 0 ; j < N - 1 - i ; j++ )
{
if( intArray[j] > intArray[j+1] )
{
int temp = intArray[j];
intArray[j] = intArray[j+1];
intArray[j+1] = temp;
}
}
}
int m = 0 ;
int n = N - k;
for ( int i = 0 ; i < N ; i++)
{
if (intArray[i]%2 == 0)
{
if ( m < N - k)
{
intArray01[m++] = intArray[i];
}
}
else
{
if ( n < N )
{
intArray01[n++] = intArray[i];
}
}
}
for ( int i = 0 ; i < N ; i++)
{
intArray[i] = intArray01[i];
System.out.println(intArray[i]);
}
System.out.println("done");
}
/*
* 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.
*/
/**
*
* @author Admin
*/
public class Main {
public static void main(String[] args)
{
Student stdA = new Student();
Student stdB = new Student();
stdA.input();
stdB.input();
String searching;
Scanner scan = new Scanner(System.in);
int choice ;
do
{
show();
choice = Integer.parseInt(scan.nextLine());
switch(choice)
{
case 1 :
searching = scan.nextLine();
if(searching.equals(stdA.getName()))
{
stdA.display();
}
else if (searching.equals(stdB.getName()))
{
stdB.display();
}
else
{
System.out.println("Không trùng tên");
}
break;
case 2 :
searching = scan.nextLine();
if(searching.equals(stdA.getAddress()))
{
stdA.display();
}
else if (searching.equals(stdB.getAddress()))
{
stdB.display();
}
else
{
System.out.println("Không trùng địa chỉ");
}
break;
case 3 :
searching = scan.nextLine();
if(searching.equals(stdA.getEmail()))
{
stdA.display();
}
else if (searching.equals(stdB.getEmail()))
{
stdB.display();
}
else
{
System.out.println("Không trùng email");
}
break;
case 4 :
searching = scan.nextLine();
if(searching.equals(stdA.getPhonenumber()))
{
stdA.display();
}
else if (searching.equals(stdB.getPhonenumber()))
{
stdB.display();
}
else
{
System.out.println("Không trùng phonenumber");
}
break;
case 5 :
int searching = Integer.parseInt(scan.nextLine());
if(searching == stdA.getAge())
{
stdA.display();
}
else if (searching == stdB.getAge())
{
stdB.display();
}
else
{
System.out.println("Không trùng tuổi");
}
break;
case 6 :
System.out.println("exit");
default :
System.out.println("break");
break;
}
}while(choice != 6);
}
public static void show()
{
System.out.println("1. search name");
System.out.println("2. search address");
System.out.println("3. search email");
System.out.println("4. search phonenumber");
System.out.println("5. search age");
System.out.println("6. exit");
}
}
![Nguyễn Văn Quang [T1907A]](https://www.gravatar.com/avatar/e40ab58e34debd5a0dbf4bcfa90bded0.jpg?s=80&d=mm&r=g)
Nguyễn Văn Quang
2020-05-11 08:14:15
package testfd2;
import java.util.Scanner;
public class JavaTestApplication {
public static void main(String[] args) {
int t ;
Scanner scan = new Scanner(System.in);
t = Integer.parseInt(scan.nextLine());
int[] intArray = new int[t] ;
int numberofodd = 0 ;
int[] intArray01 = new int[t];
for ( int i = 0 ; i < t ; i++ )
{
intArray[i] = scan.nextInt();
}
for ( int i = 0 ; i < intArray.length ; i++)
{
if ( intArray[i]%2 == 1)
{
numberofodd++;
}
}
for ( int i = 0 ; i < t ; i++)
{
System.out.println(intArray[i]);
}
System.out.println("end");
int m = 0 ;
int n = t - numberofodd;
for ( int i = 0 ; i < t ; i++)
{
if (intArray[i]%2 == 0)
{
if ( m < t - numberofodd)
{
intArray01[m++] = intArray[i];
}
}
else
{
if ( n < t )
{
intArray01[n++] = intArray[i];
}
}
}
for ( int i = 0 ; i < t ; i++)
{
intArray[i] = intArray01[i];
System.out.println(intArray[i]);
}
System.out.println("end");
}
}